HDU 3405 World Islands

Problem Description

Dubai is a haven for the rich. The government of Dubai finds a good way to make money. They built a lot of artificial islands on the sea and sell them. These islands are shaped into the continents of the world, so they are called “world islands”. All islands are booked out now. The billionaires who buy these islands wants to make friends with each other, so they want these islands all be connected by bridges. Bill Gates also has booked an island, but he is the only one who doesn’t want his island to be connected with other islands, because he prefer to travel on the sea on his old landing craft which is used in the Invasion of Normandy in World War II. Fortunately, Bill doesn’t care about which island is saved for him, so Dubai government can still find the best way to build the bridges. The best way means that the total length of the bridges is minimum. In a word, if there are n islands, what they should do is to build n–2 bridges connecting n-1 islands, and give the rest island to Bill Gates. They can give any island to Bill Gates. Now they pay you good money to help them to find out the best way to build the bridges.
Please note;
1.An island can be considered as a point.
2.A bridge can be considered as a line segment connecting two islands.
3.A bridge connects with other bridges only at the islands.

Input

The first line is an integer indicating the number of test cases.
For each test case, the first line is an integer n representing the number of islands.(0<n<50)
Then n lines follow. Each line contains two integers x and y( -20 <= x, y <= 20 ) , indicating the coordinate of an island.

Output

For each test case, output the minimum total length of the bridges in a line. The results should be rounded to 2 digits after decimal point, and you must keep 2 digits after the decimal point.

Sample Input

2
5
0 0
1 0
18 0
0 1
1 1
3
0 0
1 0
0 1

Sample Output

3.00
1.00

思路

最小生成树求最小值
这题数据较小,所以可以遍历每一个点,每一个点都假设为不相连的点,都计算一遍最小生成树,最小值即为答案。这题只给了点的坐标,两点之间的距离需要先计算一下。
此题的最小生成树采用了kruskal算法。 点击查看kruskai算法

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
using namespace std;

struct node										//点 
{
	int x,y;
}no[51];
struct edge										//边			 
{
	int from,to;
	double dis;
}e[1500];
int fa[51];				 						//父节点		
int n,m,cnt;
double ans;

bool cmp(edge a,edge b)							//排序用,从小到大 
{
	return a.dis<b.dis;					 
}

int getfa(int i)								//找根节点			 
{
	if(fa[i]==i)
		return i;
	return getfa(fa[i]);
} 

void unionn(int u,int v)
{	
	fa[getfa(u)]=getfa(v);						//合并 
}

void kruskal(int j)								//参数是不用的点 
{
	for(int i=1;i<=n;i++)						//先将自己设为自己的父节点 
		fa[i]=i;
	sort(e+1,e+1+m,cmp);					 	//所有边排序 
	for(int i=1;i<=m;i++)
	{
		if(cnt==n-2)						
			return;
		if(e[i].from==j||e[i].to==j)			//如果一条边的端点有不用的点,则继续下一条边 
			continue;
		if(getfa(e[i].from)!=getfa(e[i].to)) 	//如果一条边的两端点根节点不同,则不会成环 
		{
			unionn(e[i].to,e[i].from);			//将两端点设为子父节点关系 
			ans+=e[i].dis;
			cnt++;
		}
	}
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		memset(fa,0,sizeof(fa));
		cnt=0;
		ans=0;
		for(int i=1;i<=n;i++)
			scanf("%d%d",&no[i].x,&no[i].y);	//存点 
		for(int i=1;i<n;i++)					//遍历计算每两个点之间的距离 
		{
			for(int j=i+1;j<=n;j++)
			{
				e[++cnt].from=i;
				e[cnt].to=j;
				e[cnt].dis=sqrt(pow((no[i].x-no[j].x),2)+pow((no[i].y-no[j].y),2));//计算距离,并存储 
			}
		}
		cnt=0;
		m=n*(n-1)/2;							//边数 
		double anss=0x3f3f3f;
		for(int i=1;i<=n;i++)					//遍历所有点 
		{
			kruskal(i);							//每一个点都有可能不用 
			anss=min(ans,anss);					//取最小值 
			ans=0;								//重置数据 
			cnt=0;
		}
		cout<<setprecision(2)<<fixed<<anss<<endl;//设置输出格式 
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值