您的位置:首页 > 其它

HDU 4463 Outlets

2015-05-21 21:14 281 查看
题目链接:Outlets

题面:


Outlets

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2323 Accepted Submission(s): 1096



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



题意:

给定若干点,已知一条边,求用最小的边和,把所有点联通。其实,就是一颗最小生成树。预先求一下任意两点间的距离,数据量比较小,prim和kruscal都行,我是直接暴力得不行....

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int road[51][51],x[51],y[51];
bool flag[51];
int dis(int x0,int y0,int x1,int y1)
{
	return (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);
} 
int main()
{
	int n,p,q,tmp,cnt,fm,to;
	double cost;
	while(scanf("%d",&n)&&n)
	{
	   cost=0;
	   cnt=2;
	   vector <int> store;
	   memset(flag,-1,sizeof(flag));
	   scanf("%d%d",&p,&q);
	   for(int i=1;i<=n;i++)
	   {
   	      scanf("%d%d",&x[i],&y[i]);	
   	   }	
   	   for(int i=1;i<n;i++)
	   {
   	     for(int j=i+1;j<=n;j++)
		 {
 			tmp=dis(x[i],y[i],x[j],y[j]);
 			road[i][j]=road[j][i]=tmp;
	     }	
   	   } 
   	   store.push_back(p);
   	   store.push_back(q);
   	   flag[p]=flag[q]=0;
   	   cost+=sqrt(1.0*road[p][q]);
   	   while(cnt<n)
   	   {
   	   	 tmp=99999999;
   	   	 for(int i=0;i<store.size();i++)
   	   	 {
 	   	     for(int j=1;j<=n;j++)
			 {
			 	if(flag[j])
			 	{
 				  if(road[store[i]][j]<tmp)
 				  {
				    tmp=road[store[i]][j];
				    fm=store[i];
				    to=j; 	
			      }
			 	}
 			 }	
   	     }
   	     cnt++;
   	     flag[to]=0;
   	     cost+=sqrt(1.0*road[fm][to]);
   	     store.push_back(to);
   	   }
   	   printf("%.2lf\n",cost);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: