Outlets
Problem Description
In China,foreign brand commodities are often much more expensive than abroad. The mainreason is that we Chinese people tend to think foreign things are better and weare willing to pay much for them. The typical example is, on the United Airlineflight, 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 shoppingin outlets. Some people buy tens of famous brand shoes and bags one time. InLas Vegas, the existing outlets can't match the demand of Chinese. So they wantto 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 toldhim that Nike store and Apple store must be directly connected by a road. Nowplease help him figure out how to minimize the total road length under thiscondition. A store can be considered as a point and a road is a line segmentconnecting two stores.
Input
There areseveral 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 arenumbered from 1 to N. The second line contains two integers p and q, indicatingthat 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 alllocated at different place. The input ends by N = 0.
Output
For each testcase, print the minimum total road length. The result should be rounded to 2digits after decimal point.
Sample Input
4
2 3
0 0
1 0
0 -1
1 -1
0
Sample Output
3.41
题意:n表示有n个商店,而接下一行则表示p与q商店之间一定要走,那么先将p与q之间的权值变为0,到输出的时候再加上即可,再接下来的n行则为n个商店的坐标,这是一题简单的最小生成数题。
代码:
#include<iostream>
#include<cmath>
using namespace std;
#define inf 1111111
struct node//结构体用来存商店的坐标,用来计算距离
{
int x,y;
}k[101];
double map[501][601];//存放坐标
double l[601];//存放权值
int vis[601];//标记点是否已生成
int n,p,q;
void prim()
{
double sum=0;
int pos=0, i, j;
double Min;
for(i=0;i<n;i++)
{
l[i]=map[0][i];
vis[i]=0;
}
vis[0]=1;
for(i=0;i<n;i++)
{
Min=inf;
for(j=0;j<n;j++)
{
if(Min>l[j]&&!vis[j])
{
Min=l[j];
pos=j;
}
}
if(Min==inf)
break;
vis[pos]=1;
sum+=Min;
for(j=0;j<n;j++)//更新未加入树中的权值
{
if(l[j]>map[pos][j]&&!vis[j])
l[j]=map[pos][j];
}
}
double s=sqrt((double)(k[p-1].x-k[q-1].x)*(k[p-1].x-k[q-1].x)+(double)(k[p-1].y-k[q-1].y)*(k[p-1].y-k[q-1].y));
//将p于q之间的权值再加回来
printf("%.2lf\n",sum+s);
}
int main()
{
while(cin>>n)
{
if(!n)
break;
int i,j;
memset(map,inf,sizeof(map));//初始化数组map
cin>>p>>q;
for(i=0;i<n;i++)
cin>>k[i].x>>k[i].y;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
map[i][j]=map[j][i]=sqrt((double)(k[i].x-k[j].x)*(k[i].x-k[j].x)+(double)(k[i].y-k[j].y)*(k[i].y-k[j].y));
//将两点之间的距离作为权值存进map中
}
map[p-1][q-1]=map[q-1][p-1]=0;//将p于q之间的权值先变为0
prim();
}
return 0;
}