POJ 1679:The Unique MST(次小生成树)
来源:互联网 发布:安全数据库 编辑:程序博客网 时间:2024/06/05 21:04
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30082 Accepted: 10769
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'.
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!
题目翻译:
给出一个连通的无向图,给出N个节点(节点编号从1~N),M条边。判断这个图的最小生成树是否唯一。如果唯一输出最小生成树的值,否则输出Not Unique!
解题思路:
求其次小生成树。
次小生成树:就是生成树中权值第二小的树(其权值可以等于最小生成树),等于时说明最小生成树不唯一。
要求次小生成树,我们可以先求出最小生成树T,在构成最小生成树的边外选择一条边u,v,那么现在就有N条边了,此图必定存在环,因此我们去掉节点 u 到节点 v
在最小生成树的路径中最大的那条边,使它重新变成N-1条边,这样可以尽可能使新的生成树的值尽量小。
#include<iostream>#include<stdio.h>#include<math.h>#include<vector>#include<string.h>#include<algorithm>#define PI 3.1415926#define inf 0x3f3f3f3fusing namespace std;const int maxn = 102;int n,m; ///n个节点m条边int maxcost[maxn][maxn]; ///maxcost[i][j]存放在最小生成树中,节点i到节点j的路径中的最大权值bool mark[maxn][maxn]; ///用来标记那些边加入了最小生成树bool vis[maxn]; ///用来标记那些节点加入已经加入最小生成树。int lowcost[maxn]; ///lowcost[i]存放与i相连的点的最小距离int Map[maxn][maxn]; ///存放地图int closest[maxn]; ///lowcost[i]存放距离i最近的点。void InitMap() ///图的初始化工作{ for(int i = 1; i <= n; i++) { Map[i][i] = 0; for(int j = i+1; j <= n; j++) { Map[i][j] = Map[j][i] = inf; } }}long long int Prim(){ int Min,u; memset(maxcost,0,sizeof(maxcost)); for(int i = 1; i <= n; i++) { lowcost[i] = Map[1][i]; closest[i] = 1; vis[i] = 0; } vis[1] = 1; for(int i = 1; i < n; i++) { Min = inf; u = 0; for(int j = 1; j <= n; j++) { if(vis[j]==0 && lowcost[j]<Min) { Min = lowcost[j]; u = j; } } if(u == 0) return -1; vis[u] = 1; int pre = closest[u]; ///在最小生成树中,u-pre只有两者一条边,权为Min maxcost[u][pre] = maxcost[pre][u] = Min; mark[u][pre] = mark[pre][u] = false; ///代表这条边是最小生成树的边,标记为false for(int j = 1; j <= n; j++) { maxcost[j][u] = max(maxcost[j][pre],maxcost[pre][u]); } for(int j = 1; j <= n; j++) { if(vis[j]==0) { if(Map[u][j]<lowcost[j]) { lowcost[j] = Map[u][j]; closest[j] = u; } } } } long long MST = 0; for(int i = 1; i <= n; i++) { MST += lowcost[i]; } return MST;}int main(){ int t; int x,y,w; cin>>t; while(t--) { scanf("%d%d",&n,&m); ///n个节点,m条边 memset(mark,false,sizeof(mark)); InitMap(); ///初始化地图 for(int i = 1; i <= m; i++) { scanf("%d%d%d",&x,&y,&w); Map[x][y] = Map[y][x] = w; mark[x][y] = mark[y][x] = true; } long long int MST = Prim(); double temp = MST; bool Unique = true; ///假设最小生成树是唯一 for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == j) continue; /**不是构成最小生成树的边,加入最小生成树, 并减去节点i-j在最小生成树中最大的边**/ if(mark[i][j]) { long long newmst = temp+Map[i][j]-maxcost[i][j]; if(newmst == MST) { Unique = false; break; } } } if(!Unique) break; } if(Unique) printf("%lld\n",MST); else printf("Not Unique!\n"); } return 0;}
0 0
- 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 1679The 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(次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- POJ 1679-The Unique MST(次小生成树)
- POJ 1679 The Unique MST (次小生成树)
- iOS中SEL和IMP
- html和xml的区别
- (13.1.3.1)PMBOK之三:十大知识领域之整合管理
- React Native 嵌入到iOS原生应用
- 自动化用到的一些小技巧
- POJ 1679:The Unique MST(次小生成树)
- 剑指offer-面试题 20:顺时针打印矩阵
- C++作业5
- Java将远程服务器上的文件经过本地服务器中转后输出至前端
- poj 1236 Network of Schools
- Emmet(ZenCoding)语法(一)
- git的基础使用
- es6中"类"--class的方式创建对象和es5中正常用法比较
- [硬件]树莓派2代无显示器安装系统(固定IP+VNC开机自启动)