标签:
给你一张图,判断是否只有唯一一个最小生成树。
解题思路:先用prim算法找出一棵最小生成树,那么在此过程中用max【x】【y】数组来保存x,y两点之间唯一路中的最大那条边的权值,然后枚举不在最小生成树里面的边来替换。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 110; const int INF = 0x3f3f3f3f; int map[maxn][maxn], used[maxn][maxn], lowc[maxn],pre[maxn]; int n, m, vis[maxn], Max[maxn][maxn]; void init ()//初始化 { for (int i=0; i<maxn; i++) for (int j=0; j<maxn; j++) if (i == j) map[i][j] = 0; else map[i][j] = INF; memset (used, 0, sizeof(used)); memset (lowc, 0, sizeof(lowc)); memset (pre, 0, sizeof(pre)); memset (vis, 0, sizeof(vis)); } /***************************/ int prim (int s) { int ans = 0, mini, p; for (int i=1; i<=n; i++) { lowc[i] = map[s][i]; pre[i] = s; } vis[s] = 1; for (int i=1; i<n; i++) { mini = INF; for (int j=1; j<=n; j++) { if (!vis[j] && mini>lowc[j]) { mini = lowc[j]; p = j; } } ans += mini; vis[p] = 1; used[p][pre[p]] = used[pre[p]][p] = 1; for (int j=1; j<=n; j++) { if (vis[j] && p!=j) Max[p][j] = Max[j][p] = max (Max[j][pre[p]], lowc[p]);//记录p于j之间最长的边 if (!vis[j] && map[j][p]<lowc[j]) { lowc[j] = map[j][p]; pre[j] = p; } } } return ans; } /***************************/ int SMST (int ans) { int mini = INF; for (int i=1; i<=n; i++) for (int j=i+1; j<=n; j++) if (!used[i][j] && map[i][j]!=INF) mini = min (mini, ans - Max[i][j] + map[i][j]); return mini;//返回最小的次小生成树的值 } /***************************/ int main () { int t; scanf ("%d", &t); while (t --) { init (); scanf ("%d %d", &n, &m); while (m --) { int u, v, s; scanf ("%d %d %d", &u, &v, &s); map[u][v] = map[v][u] = s; } int num1 = prim(1); int num2 = SMST(num1); if (num1 != num2) printf ("%d\n", num1); else printf ("Not Unique!\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/mengzhong/p/4677296.html