标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5012 Accepted Submission(s):
3641
#include<stdio.h> #include<string.h> #define MAX 110 #define INF 0x3f3f3f int visit[MAX],map[MAX][MAX],low[MAX]; int city; void prime() { int j,i,min,next,mincost=0; memset(visit,0,sizeof(visit)); for(i=1;i<=city;i++) { low[i]=map[1][i]; } visit[1]=1; for(i=1;i<city;i++) { min=INF; for(j=1;j<=city;j++) { if(!visit[j]&&min>low[j]) { min=low[j]; next=j; } } visit[next]=1; mincost+=min; for(j=1;j<=city;j++) { if(!visit[j]&&low[j]>map[next][j]) low[j]=map[next][j]; } } printf("%d\n",mincost); } int main() { int n,m,j,i,x,y,sum,road; char a,b; while(scanf("%d",&city)&&city!=0) { for(i=1;i<=city;i++) { for(j=1;j<=city;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } getchar(); for(i=1;i<city;i++) { scanf("%c %d",&a,&road); x=a-‘A‘+1; while(road--) { getchar(); scanf("%c %d",&b,&m); y=b-‘A‘+1; map[x][y]=map[y][x]=m; } getchar(); } prime(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4542025.html