| ||||||||||
| 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 | |||||||||
这题没有问题,用scanf可以过的。用%s 就可以了
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int v1,v2,cost;
}Link;
Link L[1000];
int cmp(const void *a , const void *b)
{
return ((Link *)a) -> cost - ((Link *)b) -> cost;
}
int find(int father[],int v)
{
int t;
t=v;
while(father[t]>=0)
t=father[t];
return t;
}
int main()
{
int i,n,k,s,t,father[1000],count,sum,vf1,vf2;
char a[1000];
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
getchar();
count=0;
for(i=1;i<n;i++)
{
scanf("%s%d",a,&k);
s=a[0]-64;
while(k--)
{
scanf("%s%d",a,&t);
L[count].v1=s;
L[count].v2=a[0]-64;
L[count].cost=t;
count++;
}
gets(a);
}
qsort(L,count,sizeof(L[0]),cmp);
for(i=1;i<=n;i++)
father[i]=-1;
i=sum=0;
while(i<count)
{
vf1=find(father,L[i].v1);
vf2=find(father,L[i].v2);
if(vf1!=vf2)
{
father[vf2]=vf1;
sum+=L[i].cost;
}
i++;
}
printf("%d\n",sum);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator