标签:des style blog http color os io for
9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0
216 30
思路:裸的最小生成树,输入比较蛋疼,用%s处理。
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 = 1005; 10 11 using namespace std; 12 13 struct node{ 14 int x; 15 int y; 16 int value; 17 }a[M]; 18 19 int cun[M]; 20 21 int cmp(node a, node b) 22 { 23 return a.value < b.value; 24 } 25 26 int find(int x) 27 { 28 return cun[x] == x ? x : cun[x] = find(cun[x]); 29 } 30 31 void shu(int x, int y) 32 { 33 int p = find(x); 34 int q = find(y); 35 cun[p] = q; 36 } 37 38 int main() 39 { 40 int n, p, m; 41 char s[10]; 42 while( cin >> n ) 43 { 44 if(n == 0) 45 break; 46 int temp = 1; 47 for(int i = 1; i < n; ++i) 48 { 49 scanf("%s%d",s,&m); 50 while(m--) 51 { 52 scanf("%s%d",s,&p); 53 int flag = s[0] - ‘A‘ + 1; 54 a[temp].x = i; 55 a[temp].y = flag; 56 a[temp].value = p; 57 temp++; 58 } 59 } 60 for(int i = 0; i <= n; ++i) 61 cun[i] = i; 62 sort(a,a + temp,cmp); 63 int ans = 0; 64 for(int i = 1; i < temp; ++i) 65 { 66 if(find(a[i].x) != find(a[i].y)) 67 { 68 shu(a[i].x,a[i].y); 69 ans += a[i].value; 70 } 71 } 72 cout << ans << endl; 73 } 74 return 0; 75 }
hdu 1301 Jungle Roads(最小生成树),布布扣,bubuko.com
标签:des style blog http color os io for
原文地址:http://www.cnblogs.com/Unico-liyang/p/3894901.html