标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26454 | Accepted: 9457 |
Description
Input
Output
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
题意:判断最小生成树是否唯一
题解:直接求次小生成树。
次小生成树求法戳这里:http://blog.csdn.net/niushuai666/article/details/6925258
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 505; const int INF = 99999999; int graph[N][N]; int n,m; int path[N][N],pre[N],low[N]; ///path[i][j]用于记录i到j路径上的权值最大的边 bool vis[N],used[N][N]; int prim(int pos,int n){ memset(used,false,sizeof(used)); memset(vis,false,sizeof(vis)); memset(path,0,sizeof(path)); vis[pos]=true; int cost = 0; for(int i=1;i<=n;i++){ low[i]= graph[pos][i]; pre[i]=1; } low[pos]=1; for(int i=1;i<n;i++){ int Min = INF; for(int j=1;j<=n;j++){ if(!vis[j]&&low[j]<Min){ pos = j; Min = low[j]; } } used[pre[pos]][pos] = used[pos][pre[pos]] = true; cost+= Min; vis[pos] = true; for(int j=1;j<=n;j++){ if(vis[j]&&j!=pos){ ///求从pos - j路径上最大权的边 path[pos][j] = path[j][pos] = max(low[pos],path[j][pre[pos]]); } if(!vis[j]&&low[j]>graph[pos][j]){ low[j]=graph[pos][j]; pre[j] = pos; } } } return cost; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) { if(i==j) graph[i][j]=0; else graph[i][j] = INF; } } for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); graph[a][b] = graph[b][a] = c; } int cost = prim(1,n); int res = INF; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(!used[i][j]) res = min(res,cost+graph[i][j]-path[i][j]); } } if(res==cost) printf("Not Unique!\n"); else printf("%d\n",cost); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5478600.html