poj1251

/*
分析:
    最小生成树果题。


                        2012-07-30 14:17
*/








#include"stdio.h"
#include"string.h"
#include"stdlib.h"


int n;
struct A
{
	int a,b;
	int len;
}eage[1111];
int K;


int cmp(const void *a,const void *b)
{
	struct A *c,*d;
	c=(struct A *)a;
	d=(struct A *)b;
	return c->len-d->len;
}
int Prim()
{
	int ans;
	int i;
	int hash[111];


	memset(hash,0,sizeof(hash));


	ans=0;
	hash[eage[0].a]=1;
	for(i=0;i<K;i++)
	{
		if(hash[eage[i].a]+hash[eage[i].b]==1)
		{
			hash[eage[i].a]=hash[eage[i].b]=1;
			ans+=eage[i].len;
			i=-1;
		}
	}
	return ans;
}
int main()
{
	int i;
	int m;
	int a,b,c;
	char str[5];
	while(scanf("%d",&n),n)
	{
		K=0;
		for(i=1;i<n;i++)
		{
			scanf("%s",str);
			a=str[0]-'A';
			scanf("%d",&m);
			while(m--)
			{
				scanf("%s%d",str,&c);
				b=str[0]-'A';
				eage[K].a=a;
				eage[K].b=b;
				eage[K++].len=c;
			}
		}
		qsort(eage,K,sizeof(eage[0]),cmp);


		printf("%d\n",Prim());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值