标签:
1 4 6 1 2 10 2 3 10 3 1 10 1 4 1 2 4 1 3 4 1 1 3 5 6
4
1 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 int father[550], num[550]; 7 using namespace std; 8 int n, m; 9 struct rode 10 { 11 int a, b, w; 12 } nm[500*250]; 13 14 bool cmp(rode a, rode b) 15 { 16 return a.w < b.w; 17 } 18 19 void init() 20 { 21 for(int i = 1; i <= n; i++) 22 father[i] = i; 23 } 24 25 int find(int a) 26 { 27 while(a != father[a]) 28 a = father[a]; 29 return a; 30 } 31 32 bool mercy(int a, int b) 33 { 34 int q = find(a); 35 int p = find(b); 36 if(q != p) 37 { 38 father[q] = p; 39 return true; 40 } 41 else 42 return false; 43 } 44 45 int main() 46 { 47 int t; 48 scanf("%d", &t); 49 while(t--){ 50 // int n, m; 51 scanf("%d %d", &n, &m); 52 init(); 53 int sum = 0; 54 for(int i = 0; i < m; i++) 55 scanf("%d %d %d", &nm[i].a, &nm[i].b, &nm[i].w); 56 sort(nm, nm+m, cmp); 57 for(int i = 0; i < m; i++) 58 if(mercy(nm[i].a, nm[i].b)) 59 sum += nm[i].w; 60 for(int i = 0; i < n; i++) 61 scanf("%d", &num[i]); 62 sort(num, num+n); 63 sum += num[0]; 64 printf("%d\n", sum); 65 } 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4729140.html