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

POJ 1679 The Unique MST

时间:2014-07-21 00:23:08      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19847   Accepted: 6959

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

 
解题:测试数据好像有问题啊,大大的问题。。。下面写法貌似只能过这题
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 #define INF 0x3f3f3f
11 using namespace std;
12 int mp[101][101],d[101];
13 int n,m;
14 bool vis[101];
15 int prim(){
16     int i,j,k,temp,index,ans = 0;
17     for(i = 1; i <= n; i++) d[i] = INF;
18     d[1] = 0;
19     memset(vis,false,sizeof(vis));
20     for(i = 0; i < n; i++){
21         temp = INF;
22         for(j = 1; j <= n; j++)
23             if(!vis[j] && d[j] < temp) temp = d[index = j];
24         k = 0;
25         for(j = 1; j <= n; j++){
26             if(vis[j] && mp[index][j] == temp) k++;
27         }
28         if(k > 1) return -1;
29         vis[index] = true;
30         ans += temp;
31         for(j = 1; j <= n; j++){
32             if(!vis[j] && d[j] > mp[index][j]) d[j] = mp[index][j];
33         }
34     }
35     return ans;
36 }
37 int main(){
38     int ks,i,j,u,v,w;
39     scanf("%d",&ks);
40     while(ks--){
41         scanf("%d%d",&n,&m);
42         for(i = 0; i <= n; i++)
43             for(j = 0; j <= n; j++)
44             mp[i][j] = INF;
45         for(i = 0; i < m; i++){
46             scanf("%d%d%d",&u,&v,&w);
47             if(mp[u][v] > w){
48                 mp[u][v] = mp[v][u] = w;
49             }
50         }
51         w = prim();
52         if(w < 0) puts("Not Unique!");
53         else printf("%d\n",w);
54     }
55     return 0;
56 }
View Code

 

POJ 1679 The Unique MST,布布扣,bubuko.com

POJ 1679 The Unique MST

标签:des   style   blog   http   color   strong   

原文地址:http://www.cnblogs.com/crackpotisback/p/3856991.html

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