码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1679 prime次小生成树判定生成树是否唯一

时间:2015-03-17 00:45:15      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21931   Accepted: 7784

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!

题目意思:

给一个图,判定最小生成树是否唯一。prime次小生成树模板判定。

 

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9 
10 #define N 105
11 #define inf 999999999
12 
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16 
17 int maxh[N][N];
18 int map[N][N];
19 int fa[N];
20 bool in[N];
21 int stack[N];
22 int n, m;
23 int dis[N];
24 
25 int prime(){
26     int sum=0;
27     int top=0;
28     int i, j, k;
29     for(i=2;i<=n;i++){
30         dis[i]=map[1][i];fa[i]=1;
31     }
32     memset(in,false,sizeof(in));
33     dis[1]=0;
34 
35     in[1]=true;
36     stack[top++]=1;
37     for(i=1;i<=n;i++){
38         int minh=-1;
39         for(j=1;j<=n;j++){
40             if(!in[j]&&(minh==-1||minh>dis[j])){
41                 k=j;minh=dis[j];
42             }
43 
44         }//printf("%d %d\n",minh,sum);
45         if(minh==-1) return sum;
46         
47         in[k]=true;
48         sum+=minh;
49 
50         for(j=0;j<top;j++) maxh[stack[j]][k]=maxh[k][stack[j]]=max(maxh[stack[j]][fa[k]],minh);
51         stack[top++]=k;
52         for(j=1;j<=n;j++){
53             if(!in[j]&&dis[j]>map[k][j]){
54                 dis[j]=map[k][j];
55                 fa[j]=k;
56             }
57         }
58     }
59 }
60 
61 main()
62 {
63     int t, i, j, k;
64     cin>>t;
65     while(t--){
66         scanf("%d %d",&n,&m);
67         for(i=1;i<=n;i++){
68             for(j=1;j<=n;j++){
69                 map[i][j]=inf;
70                 maxh[i][j]=0;
71             }
72         }
73         int x, y, z;
74         while(m--){
75             scanf("%d %d %d",&x,&y,&z);
76             map[x][y]=map[y][x]=z;
77         }
78         int ans=prime();
79         //printf("%d\n",ans);
80         int minh=inf;
81         for(i=1;i<=n;i++){
82             for(j=1;j<=n;j++){
83                 if(i!=j&&fa[i]!=j&&fa[j]!=i)
84                     minh=min(minh,map[i][j]-maxh[i][j]);
85             }
86         }
87         if(minh==0) printf("Not Unique!\n");
88         else printf("%d\n",ans);
89     }
90 }

 

POJ 1679 prime次小生成树判定生成树是否唯一

标签:

原文地址:http://www.cnblogs.com/qq1012662902/p/4343340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!