标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20024 | Accepted: 9234 |
Description
Input
Output
Sample Input
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
Sample Output
216 30
Source
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<algorithm> #define maxn 110 #define INF 1000000 using namespace std; int n; int Edge[maxn][maxn]; int path[maxn]; int vis[maxn]; int sum = 0; void prime(int u0) { memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) { path[i] = Edge[u0][i]; } vis[u0] = 1; sum = 0; for(int i=1; i<=n; i++) { int tt,mmin = INF; for(int j=1; j<=n; j++) { if(vis[j] == 0 && path[j] < mmin) { mmin = path[j]; tt = j; } } if(mmin == INF) break; sum += path[tt]; vis[tt] = 1; for(int k=1; k<=n; k++) { if(vis[k] == 0 && path[k] > Edge[tt][k]) { path[k] = Edge[tt][k]; } } } printf("%d\n",sum); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(~scanf("%d",&n)) { if(n == 0) break; char ch,ch1; int u,v; int w,Q; memset(Edge,INF,sizeof(Edge)); memset(path,INF,sizeof(path)); for(int i=1; i<n; i++) { cin>>ch1>>Q; u = ch1 - 64; for(int j=1; j<=Q; j++) { cin>>ch1>>w; v = ch1 - 64; Edge[u][v] = w; Edge[v][u] = w; } } prime(1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4355381.html