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

poj1679 次最小生成树 kruskal(暴力枚举)

时间:2018-05-23 23:31:56      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:标记   tput   lse   namespace   use   lag   style   bin   str   

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!

这题直接暴力枚举,找到一颗最小生成树,标记一下,依次删边


 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 using namespace std;
 8 const int maxn = 1010 ;
 9 const int INF = 0x7fffffff;
10 struct node {
11     int u, v, w;
12     int used, num, flag;
13 } qu[10 * maxn];
14 int fa[maxn], n, m;
15 int cmp(node a, node b) {
16     return a.w < b.w;
17 }
18 void init() {
19     for (int i = 0 ; i <= n ; i++) fa[i] = i;
20 }
21 int Find(int x) {
22     return fa[x] == x ? x : fa[x] = Find(fa[x]);
23 }
24 int combine(int x, int y) {
25     int nx = Find(x);
26     int ny = Find(y);
27     if (nx != ny) {
28         fa[nx] = ny;
29         return 1;
30     }
31     return 0;
32 }
33 int kruskal(int flag) {
34     init();
35     int sum = 0, cnt = 0;
36     for (int i = 0 ; i < m ; i++) {
37         if (qu[i].flag) continue;
38         if (combine(qu[i].v, qu[i].u)) {
39             if (!flag) qu[i].used = 1;
40             sum += qu[i].w;
41             cnt++;
42             if (cnt == n - 1) break;
43         }
44     }
45     if (cnt != n - 1) return -1;
46     return sum;
47 }
48 int main() {
49     int t;
50     scanf("%d", &t);
51     while(t--) {
52         scanf("%d%d", &n, &m);
53         for (int  i = 0 ; i < m ; i++) {
54             scanf("%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w);
55             qu[i].flag = 0, qu[i].used = 0;
56         }
57         sort(qu, qu + m, cmp);
58         int sum = kruskal(0);
59         int flag = 0;
60         for (int i = 0 ; i < m ; i++) {
61             if (qu[i].used ) {
62                 qu[i].flag = 1;
63                 int temp = kruskal(1);
64                 qu[i].flag = 0;
65                 if (temp == sum) {
66                     flag = 1;
67                     break;
68                 }
69             }
70         }
71         if (flag) printf("Not Unique!\n");
72         else printf("%d\n", sum);
73     }
74     return 0;
75 }

 




poj1679 次最小生成树 kruskal(暴力枚举)

标签:标记   tput   lse   namespace   use   lag   style   bin   str   

原文地址:https://www.cnblogs.com/qldabiaoge/p/9080058.html

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