zoj 2048

输出的时候好像要排下序~ 

If no new highways need to be built (all towns are already connected), then the output should be created but it should be empty.

起初的时候把题目中的这句话理解错了,the block should be empty 是什么操作也没有,我起初以为是要输出一个空白行 = = !

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct roadl
{
	int x;
	int y;
	double len;
}road[600000];
int cmp(const void *a,const void *b)
{
	struct  roadl *p1,*p2;
	p1=(struct roadl *)a;
	p2=(struct roadl *)b;
	if((*p1).len!=(*p2).len)                      //二级排序,如果长度一样,按编号排序
		return (*p1).len > (*p2).len ? 1:-1;
	else
	{
		if((*p1).x > (*p2).x)
			return (*p1).x - (*p2).x;
	}
}
int find(int x,int *father)
{
	if(father[x]==x)
		return x;
	else
		return find(father[x],father);
}
int main()
{
	int m,n,i,j,a,b,t,k,cas,A,B;
	int p[760][2],father[760];
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d%d",&p[i][0],&p[i][1]);
		for(i=0;i<=n;i++)
			father[i]=i;
		for(i=0,k=0;i<n-1;i++)
		{
			for(j=i+1;j<n;j++)
			{
				road[k].x=i+1;
				road[k].y=j+1;
				road[k].len=sqrt((p[i+1][0]-p[j+1][0])*(p[i+1][0]-p[j+1][0])+(p[i+1][1]-p[j+1][1])*(p[i+1][1]-p[j+1][1]));  //计算两点间的距离
				k++;
			}
		}
		scanf("%d",&m);
		for(i=0,t=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			A=find(a,father);
			B=find(b,father);
			if(A!=B)
			{
				father[A]=B;   //连接已建有路的点
				t++;
			}
		}
		qsort(road,k,sizeof(road[0]),cmp);  //排序
		for(i=0;i<k&&t<n-1;i++)
		{
			A=find(road[i].x,father);
			B=find(road[i].y,father);
			if(A!=B)
			{
				father[A]=B;
				if(road[i].len!=0)
					printf("%d %d\n",road[i].x,road[i].y);
				t++;
			}
		}
		if(cas>0)
			printf("\n");
	}
	return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值