Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
测试数据都过了,为神马RE。。。伤心!求大神指点#include<stdio.h> #include<algorithm> #include<stdlib.h> #include <iostream> using namespace std; int p[30]; int r[110]; int u[110]; int v[110]; int w[110]; int cmp_set(const int i, const int j) { return w[i] < w[j]; } void init_set(int n) { int i; int m = n * (n - 1) / 2; for(i = 0; i < n; i++) p[i] = i; for(i = 0; i < m; i++) r[i] = i; } int find_set(int x) { if(x != p[x]) p[x] = find_set(p[x]); return p[x]; } int main() { int i; int cnt; int j; int n; char place[2]; char place2[2]; int dis; int t; int m; int e; int x; int y; int ans; int cou; while(scanf("%d", &n), n) { t = 0; cou = 0; m = 0; ans = 0; for(i = 0; i < n - 1; i++) { scanf("%s%d", place, &cnt); m += cnt; while(cnt--) { scanf("%s%d", place2, &dis); u[t] = place[0] - 'A'; v[t] = place2[0] - 'A'; w[t] = dis; t++; } } init_set(n); sort(r, r + m, cmp_set); i = 0; for(i = 0; i < m; i++) { e = r[i]; x = find_set(u[e]); y = find_set(v[e]); if(x != y) { ans += w[e]; p[x] = y; cou++; } } cout<<ans<<endl; } system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator