zoj 2326

 最小生成树题目……

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct paths
{
	char x[21];
	char y[21];
	float dis;
}path[400];
int cmp(const void *a,const void *b) //这里注意啦,qsort返回值是int型的,必然不能返回两数的差值!!!
{
	return (*(struct paths*)a).dis>(*(struct paths*)b).dis?1:-1;
}
int find(int x,int *father)
{
	if(father[x]==x)
		return x;
	else
		return find(father[x],father);
}
int main()
{
	int m,n,j,father[400],i,A,B;
	char a[400][21];
	float len,count;
	scanf("%f",&len);
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%s",a[i]);
	for(i=0;i<n;i++)
		father[i]=i;
	scanf("%d",&m);
	for(i=0;i<m;i++)
		scanf("%s%s%f",&path[i].x,&path[i].y,&path[i].dis);
	qsort(path,m,sizeof(path[0]),cmp);
	for(i=0,count=0;i<m;i++)
	{
		for(j=0;j<n&&strcmp(a[j],path[i].x)!=0;j++)
		{}
		A=find(j,father);
		for(j=0;j<n&&strcmp(a[j],path[i].y)!=0;j++)
		{}
		B=find(j,father);
		if(A!=B)
		{
			father[A]=B;
			count+=path[i].dis;
		}
	}
	if(count<=len)
		printf("Need %.1f miles of cable\n",count);
	else
		printf("Not enough cable\n");
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值