(有条件)最短路

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
42 30 01 00 -1 1 -10
 

Sample Output
3.41


题意概况:在一个城市有n个商店编号从1—n,可以买各种东西,现在给你n个城市的坐标,耐克和苹果店的编号。这两个店直接必须有一条直接联系的路。问把这些商店逛完最短需要走多远?

解题思路:通过给出的n个点的坐标算出这n个点之间 的距离,然后选择合适的距离,其实就是一个最小生成树问题,不过因为题目规定的两个点之间必须存在一条路,但是如果这两个点连接的直线上还有其他商店那么会多加上一些距离,所以我们需要先找到在这条线上是否存在其他点,把这些点删去当完全不存在即可。

注意:数的范围,尽量多用double

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define N 0x3f3f3f3f
using namespace std;
double map[500][500];
int vis[500];
double s[500];
int n;
int p,q;
struct ac
{
	int a,b;
}r[202];
void prime()
{
	memset(vis,0,sizeof(vis));
	for(int i=0;i<n;i++)
	{
		s[i]=map[0][i];
	}
	vis[0]=1;
	double sum=0;
	for(int i=0;i<n;i++)
	{
		double minn=N;
		int u=-1;
		for(int j=0;j<n;j++)
		{
			if(s[j]<minn&&!vis[j])
			{
				minn=s[j];
				u=j;
			}
		}
		if(minn==N)
		break;
		sum+=minn;
		vis[u]=1;
		for(int j=0;j<n;j++)
		{
			if(s[j]>map[u][j]&&!vis[j])
			{
				s[j]=map[u][j];
			}
		}
	}
	double s=sqrt((double)(r[p-1].a-r[q-1].a)*(r[p-1].a-r[q-1].a)+(double)(r[p-1].b-r[q-1].b)*(r[p-1].b-r[q-1].b));
	printf("%.2lf\n",sum+s);
}
int main()
{
	while(cin>>n)
	{
		if(n==0)
		break;
		cin>>p>>q;
		memset(map,N,sizeof(map));
		for(int i=0;i<n;i++)
		{
			cin>>r[i].a>>r[i].b;
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				map[i][j]=map[j][i]=sqrt((double)(r[i].a-r[j].a)*(r[i].a-r[j].a)+(double)(r[i].b-r[j].b)*(r[i].b-r[j].b));
			}
		}
		map[p-1][q-1]=map[q-1][p-1]=0;
		prime();
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/henucm/article/details/80996174