Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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.
Input
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.
Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
Sample Input
4
2 3
0 0
1 0
0 -1
1 -1
0
Sample Output
3.41
Source
2012 Asia Hangzhou Regional Contest
题意不难,n个商店吧,给出这n个商店的坐标,其中p和q号商店必须相连,求联通所有商店的最小路长;
数据范围都很小,显然是一个最小生成树问题,先把所有商店间的距离求出来,然后就是一个纯的kruskal了,只不过我们在用并查集的时候先见p和q号商店连接起来;
#include
#include
#include
#include
#include
using namespace std;
const int N=2500+10;
int t,n,m,k,a[N],b[N],f[N];
double s;
struct node
{
int x,y;//联通的两个点;
double w;//存两点间的距离;
}c[N];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
return f[x]==-1?x:f[x]=find(f[x]);
}
double ks()
{
int i;
memset(f,-1,sizeof(f));
int x=find(n),y=find(m);
f[x]=y;
sort(c,c+k,cmp);
for(i=0;i<k;i++)
{
int x=find(c[i].x);
int y=find(c[i].y);
if(x!=y)
{
s+=c[i].w;
f[x]=y;
}
}
return s;
}
int main()
{
int i,j;
while(~scanf("%d",&t)&&t)
{
scanf("%d%d",&n,&m);
s=0,k=0;
for(i=1;i<=t;i++)
{
scanf("%d%d",&a[i],&b[i]);
for(j=1;j<i;j++)
{
c[k].x=j,c[k].y=i;//将两个点也存起来;
c[k++].w=hypot(a[j]-a[i],b[j]-b[i]);//紫书第62页有这个函数的用法,可以去看看,很方便;
if((i==n&&j==m)||(i==m&&j==n))
s+=hypot(a[j]-a[i],b[j]-b[i]);
}
}
printf("%.2f\n",ks());
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章