POJ-1679.The Unique MST.(Prim求次小生成树)

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39561   Accepted: 14444

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

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!

Source

 
本题大意:给定一个无向图,让你判断这个图的最小生成树是否唯一。
本题思路:我们可以求出这个图的次小生成树,判断如果Second_MST > MST则说明这个图G只存在一颗最小生成树,相等则说明存在不止一颗最小生成树。
参考代码:
Prim解法:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int maxn = 100 + 5, maxe = 100 * 100 / 2 + 5, INF = 0x3f3f3f3f;
 7 int n, m, lowc[maxn], pre[maxn], cost[maxn][maxn], Max[maxn][maxn];
 8 bool vis[maxn], used[maxn][maxn];
 9 
10 int Prim(int source) {
11     int ans = 0;
12     memset(vis, false, sizeof vis);
13     memset(Max, 0, sizeof Max);
14     memset(used, false, sizeof used);
15     for(int i = 2; i <= n; i ++) {
16         lowc[i] = cost[source][i];
17         pre[i] = source;
18     }
19     pre[source] = -1;
20     lowc[source] = 0;
21     vis[source] = true;
22     for(int i = 2; i <= n; i ++) {
23         int MIN = INF, k = -1;
24         for(int j = 1; j <= n; j ++)
25             if(!vis[j] && MIN > lowc[j]) {
26                 MIN = lowc[j];
27                 k = j;
28             }
29         if(MIN == INF) return -1;
30         vis[k] = true;
31         ans += MIN;
32         used[pre[k]][k] = used[k][pre[k]] = true;//这里记得要把现在访问的边进行标记
33         for(int j = 1; j <= n; j ++) {
34             if(vis[j] && j != k)
35                 Max[k][j] = Max[j][k] = max(Max[pre[k]][j], lowc[k]);//每次加入一个顶点,就将这个顶点到达其他顶点路径上的最大边权进行更新
36                 //为什么要这样更新呢?我们知道一个顶点在还没有加入最小生成树时它距离MST中各边顶点的最小值可以由它的父亲结点到j结点和它本身到MST结点的最小值的最大值来表示
37             if(!vis[j] && lowc[j] > cost[k][j]) {
38                 lowc[j] = cost[k][j];
39                 pre[j] = k;
40             }
41         }
42     }
43     return ans;
44 }
45 
46 int Second_Prim(int MST) {
47     int ans = INF;
48     for(int i = 1; i <= n; i ++)
49         for(int j = 1 + i; j <= n; j ++)
50             if(!used[i][j] && cost[i][j] != INF)
51                 ans = min(ans, MST - Max[i][j] + cost[i][j]);
52     return ans;
53 }
54 
55 int main () {
56     int t, u, v, w;
57     scanf("%d", &t);
58     while(t --) {
59         scanf("%d %d", &n, &m);
60         memset(cost, INF, sizeof cost);
61         for(int i = 1; i <= m; i ++) {
62             scanf("%d %d %d", &u, &v, &w);
63             cost[u][v] = cost[v][u] = w;
64         }
65         int MST = Prim(1);
66         int Second_MST = Second_Prim(MST);
67         if(Second_MST > MST)
68             printf("%d\n", MST);
69         else printf("Not Unique!\n");
70     }
71     return 0;
72 }

 

posted @ 2019-05-08 16:39  Cruel_King  阅读(260)  评论(0编辑  收藏  举报