POJ 1679 The Unique MST(次小生成树之一)
来源:互联网 发布:源码助手 编辑:程序博客网 时间:2024/05/26 20:23
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!
题目大意
对于一个无向图,判断其最小生成树是否唯一,若唯一,输出最小生成树,若不唯一,输出Not Unique!。
解题思路
次小生成数求解,对于无向图,求次小生成树,判断其值是否与最小生成树相等,若相等,则最小生成树不唯一;相反,则唯一。
次小生成树求解思想:
1、先求出最小生成树,在这一过程中,标记出最小生成树包含的边,并且保存在最小生成树中从节点i到节点j路径中的最大边权Max[i][j];
2、选取不在最小生成树中的一条边,添加进去,那么一定会构成一个环,然后除去这个环中边(除了刚添加的边)的最大值(即Max[i][j]),当前仍是无向图的一个生成树且该生成树有可能是次小生成树;
3、重复步骤2,依次将不在最小生成树中的边遍历完之后,得到的最小值便是次小生成树。
代码实现
#include <iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 207#define INF 0x3f3f3f3fint cost[maxn][maxn],used[maxn][maxn];int pre[maxn],lowcost[maxn],Max[maxn][maxn];bool vis[maxn];int n,m,ans;int Prim(){ memset(vis,0,sizeof(vis)); memset(Max,0,sizeof(Max)); memset(lowcost,INF,sizeof(lowcost)); memset(used,0,sizeof(used)); vis[0]=1; //先将节点0添加到最小生成树中 pre[0]=-1; int an=0; for(int i=1; i<n; i++) { lowcost[i]=cost[0][i]; pre[i]=0; } for(int i=1; i<n; i++) { int Min=INF; int p=-1; for(int j=0; j<n; j++) //寻找最短的边添加到最小生成树中 if(!vis[j]&&Min>lowcost[j]) { Min=lowcost[j]; p=j; } an+=Min; //最小生成树的值 vis[p]=1; //将节点p添加到最小生成树中 used[p][pre[p]]=used[pre[p]][p]=1; //标记最小生成树包含的边 for(int j=0; j<n; j++) { if(vis[j]) Max[j][p]=Max[p][j]=max(Max[j][pre[p]],cost[j][p]); //更新最小生成树中从i到j路径中的最大边权 else { if(lowcost[j]>cost[j][p]) //更新还未添加到最小生成树中的节点与现有的最小生成树的最短距离 { lowcost[j]=cost[j][p]; pre[j]=p; } } } } return an;}int smst(){ int cnt=INF; for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(cost[i][j]!=INF&&!used[i][j]) //添加不在最小生成树的边,减去环中的最大边权,更新次小生成树 { cnt=min(cnt,ans+cost[i][j]-lowcost[j]); } } } return cnt;}int main(){ int T; int u,v,l; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); for(int i=0; i<n; i++) //初始化 for(int j=0; j<n; j++) { if(i==j) cost[i][j]=0; else cost[i][j]=INF; } for(int i=0; i<m; i++) { scanf("%d %d %d",&u,&v,&l); u--,v--; //节点从0开始 cost[u][v]=cost[v][u]=l; } ans=Prim(); //求最小生成树 if(ans==smst()) //求次小生成树进行比较 printf("Not Unique!\n"); else printf("%d\n",ans); } return 0;}
- POJ 1679 The Unique MST(次小生成树之一)
- poj 1679 - The Unique MST ( 次小生成树 )
- POJ 1679 The Unique MST 次小生成树Prim
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树
- poj 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST 次小生成树
- 【次小生成树】POJ-1679 The Unique MST
- poj 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST(次小生成树&Kruskal)
- poj 1679 The Unique MST 次小生成树算法
- POJ 1679 The Unique MST (次小生成树)
- poj 1679 The Unique MST 次小生成树
- The Unique MST - POJ 1679 次小生成树
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树 裸题
- 在数据处理中常用的5种概率分布的python实现
- 微信小程序相关
- BZOJ1098: [POI2007]办公楼biu
- oracle中数据是怎样前滚和回滚的
- HMI全液晶仪表方案-使用QNX和Kanzi的UI解决方案
- POJ 1679 The Unique MST(次小生成树之一)
- Jobdu1172 哈夫曼树
- java修改jdk不起作用
- redis的安装以及增添到window服务里面
- 使用shader实现UI按钮置灰的操作
- Objective-C: ARC和Non-ARC混编的问题
- thinkphp 3.2 钩子原理
- Git与Github的区别
- Sum of a subsequence 多次随机化