Description
Input
Output
Sample Input
Sample Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 |
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using
namespace std; const
int MAXX = 100005; int
father[MAXX]; bool
vis[MAXX]; struct
kruskal { int
a; int
b; int
value; }; kruskal edge[MAXX]; int
find( int
x) { return
x==father[x]?x:find(father[x]); } bool
unionsearch( int
a, int
b) { int
fa = find (a); int
fb = find (b); if (fa==fb) return
false ; else { father[fb] = fa; return
true ; } } bool
cmp( const
kruskal&a, const
kruskal&b) { return
a.value<b.value; } int
main() { int
num; while ( scanf ( "%d" ,&num)&&num) { int
ans_value =0; memset (edge,0, sizeof (kruskal)); for ( int
i =1;i<=num;i++) { father[i] = i; } int
n = num*(num-1)/2; int
N=1; for ( int
i = 1;i<=n;i++) { int
a,b,value,flag; scanf ( "%d %d %d %d" ,&a,&b,&value,&flag); if (flag) { unionsearch(a,b); } else { edge[N].a = a; edge[N].b = b; edge[N++].value = value; } } sort(edge+1,edge+N,cmp); for ( int
i = 1;i<N;i++) { if (unionsearch(edge[i].a,edge[i].b)) { ans_value +=edge[i].value; } } cout<<ans_value<<endl; } return
0; } |
原文:http://www.cnblogs.com/locojyw/p/3716145.html