首页 > ACM题库 > HDU-杭电 > HDU 4463-Outlets-图-[解题报告]HOJ
2015
07-16

HDU 4463-Outlets-图-[解题报告]HOJ

Outlets

问题描述 :

In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can’t match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.

输入:

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.

输出:

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.

样例输入:

4
2 3
0 0
1 0
0 -1 
1 -1
0

样例输出:

3.41

/*
分析:
    2012亚洲杭州站K题。
    水题。。。

                        2012-11-08
*/

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
int pre[100];

struct A
{
	int a,b,len;
}eage[3000];
int k;
int x[100],y[100];

void build(int n)
{
	int i;
	for(i=0;i<n;i++)	pre[i]=i;
}
int find(int k)
{
	if(pre[k]==k)	return k;
	pre[k]=find(pre[k]);
	return pre[k];
}
void Union(int f1,int f2)
{
	pre[f1]=f2;
}
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 main()
{
	int n;
	int i,l;
	int d1,d2;
	int f1,f2;
	double ans;
	while(scanf("%d",&n),n)
	{
		build(n);
		scanf("%d%d",&d1,&d2);
		d1--;d2--;
		for(i=0;i<n;i++)	scanf("%d%d",&x[i],&y[i]);

		k=0;
		for(i=1;i<n;i++)
		for(l=0;l<i;l++)
		{
			eage[k].a=i;
			eage[k].b=l;
			eage[k].len=(x[i]-x[l])*(x[i]-x[l])+(y[i]-y[l])*(y[i]-y[l]);
			k++;
		}
		qsort(eage,k,sizeof(eage[0]),cmp);

		ans=sqrt((double)(x[d1]-x[d2])*(x[d1]-x[d2])+(y[d1]-y[d2])*(y[d1]-y[d2]));
		Union(d1,d2);
		for(i=0;i<k;i++)
		{
			f1=find(eage[i].a);
			f2=find(eage[i].b);
			if(f1==f2)	continue;
			Union(f1,f2);
			ans+=sqrt((double)eage[i].len);
		}
		printf("%.2lf\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/8163474


,