标签:des style blog http color os io for
思路:最小生成树,没什么好说的。 注意:因为输入量较多,所以用cin输入会超时,改用scanf输入。
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<math.h> 6 #include<algorithm> 7 #define LL long long 8 #define mod 1e9 + 7 9 const int M = 105; 10 11 using namespace std; 12 13 struct node{ 14 int x; 15 int y; 16 int value; 17 }a[M * M >> 1]; 18 19 int cun[M]; 20 21 22 bool cmp(node a, node b) 23 { 24 return a.value < b.value; 25 } 26 27 int find(int x) 28 { 29 return cun[x] == x ? x : cun[x] = find(cun[x]); 30 } 31 32 void shu(int x, int y) 33 { 34 int p = find(x); 35 int q = find(y); 36 cun[p] = q; 37 } 38 39 int main() 40 { 41 int n; 42 while( cin >> n , n ) 43 { 44 int t = n * (n - 1) / 2; 45 for(int i = 0; i < t; ++i) 46 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].value); 47 for(int i = 0; i <= n; ++i) 48 cun[i] = i; 49 sort(a,a + t,cmp); 50 int ans = 0; 51 for(int i = 0; i < t; ++i) 52 { 53 if(find(a[i].x) != find(a[i].y)) 54 { 55 shu(a[i].x,a[i].y); 56 ans += a[i].value; 57 } 58 } 59 cout << ans << endl; 60 } 61 return 0; 62 }
hdu 1233 还是畅通工程(最小生成树),布布扣,bubuko.com
标签:des style blog http color os io for
原文地址:http://www.cnblogs.com/Unico-liyang/p/3894114.html