博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1679The Unique MST(次小生成树)
阅读量:4965 次
发布时间:2019-06-12

本文共 4363 字,大约阅读时间需要 14 分钟。

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25203   Accepted: 8995

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique. 
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
1. V' = V. 
2. T is connected and acyclic. 
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique! 题意:问一棵最小生成树是否唯一 找次小生成树,如果相等不唯一,否则唯一 次小生成树:就是最小生成树换一条边而成的生成树;用maxd【x】【y】存储最小生成树两个节点(x,y)路径中最大的那条边的权值,也就是最小生成树中x-z-...-y中那个最大的那一个权值,然后就可以换边了,到底换哪一个边就从不在生成树中的边一个一个枚举咯,假设x-y,如果要用x-y替换x-z...-y肯定得替换x-z...-y中权值最大的那条边才能让最后得到结果只比x-z...-y小一点,也就是仅次于
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 const int MAX = 110; 7 const int INF = 100000000; 8 int n,m,ans,cnt; 9 int edge[MAX][MAX],vis[MAX],used[MAX][MAX],dist[MAX]; 10 int pre[MAX],maxd[MAX][MAX]; 11 void prime() 12 { 13 memset(vis,0,sizeof(vis)); 14 memset(used,false,sizeof(used)); 15 memset(maxd,0,sizeof(maxd)); 16 for(int i = 1; i <= n; i++) 17 { 18 dist[i] = edge[1][i]; 19 pre[i] = 1; 20 } 21 dist[1] = 0; 22 vis[1] = 1; 23 pre[1] = -1; 24 for(int i = 1; i < n; i++) 25 { 26 int minn = INF,pos = -1; 27 for(int j = 1; j <= n; j++) 28 { 29 if(vis[j] == 0 && dist[j] < minn) 30 { 31 minn = dist[j]; 32 pos = j; 33 } 34 } 35 if(minn == INF) 36 { 37 ans = INF; 38 return; 39 } 40 ans += minn; 41 vis[pos] = 1; 42 used[pos][pre[pos]] = used[ pre[pos] ][pos] = true; 43 for(int j = 1; j <= n; j++) 44 { 45 if(vis[j]) 46 maxd[j][pos] = maxd[pos][j] = max(dist[pos], maxd[j][pre[pos]]); 47 if(vis[j] == 0 && dist[j] > edge[pos][j]) 48 { 49 dist[j] = edge[pos][j]; 50 pre[j] = pos; 51 } 52 } 53 } 54 } 55 void smst() 56 { 57 cnt = INF; 58 for(int i = 1; i <= n; i++) 59 { 60 for(int j = i + 1; j <= n; j++) 61 { 62 if(used[i][j] == false && edge[i][j] != INF) 63 { 64 cnt = min(cnt, ans - maxd[i][j] + edge[i][j]); 65 } 66 } 67 } 68 } 69 int main() 70 { 71 int t; 72 scanf("%d", &t); 73 while(t--) 74 { 75 for(int i = 1; i <= MAX; i++) 76 for(int j = 0; j <= MAX; j++) //初始化的时候把MAX,写成了n, orz.... 77 { 78 if(i != j) 79 edge[i][j] = INF; 80 else 81 edge[i][i] = 0; 82 } 83 scanf("%d%d", &n,&m); 84 for(int i = 0; i < m; i++) 85 { 86 int x,y,w; 87 scanf("%d%d%d", &x, &y, &w); 88 edge[x][y] = edge[y][x] = w; 89 } 90 ans = 0; 91 prime(); 92 smst(); 93 if(ans == INF) 94 { 95 printf("Not Unique!\n"); 96 continue; 97 } 98 if(cnt == ans) 99 {100 printf("Not Unique!\n");101 }102 else103 {104 printf("%d\n",ans);105 }106 }107 return 0;108 }
View Code

 

 

 

转载于:https://www.cnblogs.com/zhaopAC/p/4986175.html

你可能感兴趣的文章
div中p标签自动换行
查看>>
<mvc:annotation-driven/>的作用
查看>>
服务器一:分布式服务器结构
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>
如何从亿量级中判断一个数是否存在?
查看>>
客户数据(类的调用)
查看>>
cookie session 和登录验证
查看>>
为mysql数据库建立索引
查看>>
语言-汉语-官话-中原官话-兖菏片:兖菏片
查看>>
HTML-参考手册: 画布
查看>>
杂项:MIS
查看>>
Node.js:全局对象
查看>>
6、python中的字符串
查看>>
String、StringBuffer与StringBuilder之间区别
查看>>
bash中常见环境变量env、set、export 、declare与bash漏洞原理
查看>>
Vue.js 子组件的异步加载及其生命周期控制
查看>>
数据库表结构导出sql语句
查看>>
C++库(Thrift)
查看>>