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
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; double lowcost[100]; double map[111][111]; const double inf=99999999; int vis[111]; struct node { double x; double y; }a[11111]; double fun(node x,node y) { return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y)); } int main() { int n,i,j,k,u,v; double sum,temp,max; while(scanf("%d",&n)==1 && n){ cin>>u>>v; for(i=1;i<=n;i++) { scanf("%lf%lf",&a[i].x,&a[i].y); vis[i]=0; } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++){ map[i][j]=map[j][i]=fun(a[i],a[j]); } } map[u][v]=map[v][u]=0; vis[1]=1; sum=fun(a[u],a[v]); for(i=1;i<=n;i++) lowcost[i]=map[1][i]; for(i=1;i<=n;i++) { temp=inf; for(j=1;j<=n;j++) { if(!vis[j] && lowcost[j]<temp) { k=j; temp=lowcost[j]; } } if(temp==inf) break; vis[k]=1; sum+=temp; for(j=1;j<=n;j++) { if(!vis[j] && lowcost[j]>map[k][j]) lowcost[j]=map[k][j]; } } printf("%0.2lf\n",sum); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。