1. 程式人生 > >POJ1679 The Unique MST —— 次小生成樹

POJ1679 The Unique MST —— 次小生成樹

imu 最大權值 ont them ati smallest aps other nes

題目鏈接:http://poj.org/problem?id=1679


The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31378 Accepted: 11306

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

POJ Monthly--2004.06.27 srbga@POJ


題解:

問:最小生成樹是否唯一。

次小生成樹模板題。

代碼如下:

技術分享
 1 #include <iostream>
 2
#include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 typedef long long LL; 7 const double EPS = 1e-6; 8 const int INF = 2e9; 9 const LL LNF = 9e18; 10 const int MOD = 1e9+7; 11 const int MAXN = 1e2+10; 12 13 int cost[MAXN][MAXN], lowc[MAXN], pre[MAXN], Max[MAXN][MAXN]; 14 bool vis[MAXN], used[MAXN][MAXN]; 15 16 int Prim(int st, int n) 17 { 18 int ret = 0; 19 memset(vis, false, sizeof(vis)); 20 memset(used, false, sizeof(used)); 21 memset(Max, 0, sizeof(Max)); 22 23 for(int i = 1; i<=n; i++) 24 lowc[i] = (i==st)?0:INF; 25 pre[st] = st; 26 27 for(int i = 1; i<=n; i++) 28 { 29 int k, minn = INF; 30 for(int j = 1; j<=n; j++) 31 if(!vis[j] && minn>lowc[j]) 32 minn = lowc[k=j]; 33 34 if(minn==INF) return -1; //不連通 35 vis[k] = true; 36 ret += minn; 37 used[pre[k]][k] = used[k][pre[k]] = true; //pre[k]-k的邊加入生成樹 38 for(int j = 1; j<=n; j++) 39 { 40 if(vis[j] && j!=k) //如果遇到已經加入生成樹的點,則找到兩點間路徑上的最大權值。 41 Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]); //k的上一個點是pre[k] 42 if(!vis[j] && lowc[j]>cost[k][j]) //否則,進行松弛操作 43 { 44 lowc[j] = cost[k][j]; 45 pre[j] = k; 46 } 47 } 48 } 49 return (ret==INF)?-1:ret; 50 } 51 52 int SMST(int t1 ,int n) 53 { 54 int ret = INF; 55 for(int i = 1; i<=n; i++) //用生成樹之外的一條邊去代替生成樹內的一條邊 56 for(int j = i+1; j<=n; j++) 57 { 58 if(cost[i][j]!=INF && !used[i][j]) //去掉了i-j路徑上的某條邊,但又把i、j直接連上,所以還是一棵生成樹。 59 ret = min(ret, t1+cost[i][j]-Max[i][j]); 60 } 61 return ret; 62 } 63 64 int main() 65 { 66 int T, n, m; 67 scanf("%d", &T); 68 while(T--) 69 { 70 71 scanf("%d%d",&n,&m); 72 for(int i = 1; i<=n; i++) 73 for(int j = 1; j<=n; j++) 74 cost[i][j] = (i==j)?0:INF; 75 76 for(int i = 1; i<=m; i++) 77 { 78 int u, v, w; 79 scanf("%d%d%d", &u, &v, &w); 80 cost[u][v] = cost[v][u] = w; 81 } 82 83 int t1 = Prim(1, n); 84 int t2 = SMST(t1, n); 85 if(t1!=-1 && t2!=-1 && t1!=t2) printf("%d\n", t1); 86 else printf("Not Unique!\n"); 87 } 88 }
View Code

POJ1679 The Unique MST —— 次小生成樹