HDOJ 4463 Outlets(最小生成树--prime)

Outlets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2545    Accepted Submission(s): 1174


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


费了好长时间读题理解,给了N个点的坐标,然后规定两个点必须相连,求出连线最短长度

ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#define max(a,b) a>b?a:b
#define MAXN 1010
#define INF 0xfffffff
using namespace std;
double pri[MAXN][MAXN];
int v[MAXN];
double dis[MAXN];
int t;
double sum;
struct s
{
	int x;
	int y;
}a[MAXN];
void prime()
{
	int k,i,j;
	double M;
	memset(v,0,sizeof(v));
	v[1]=1;
	sum=0;
	for(i=1;i<=t;i++)
	{
		dis[i]=pri[1][i];
	}
	for(i=0;i<t;i++)
	{
		M=INF;
		for(j=1;j<=t;j++)
		{
			if(v[j]==0&&dis[j]<M)
			{
				M=dis[j];
				k=j;
			}
		}
		if(M==INF)
		break;
		v[k]=1;
		sum+=M;
		for(j=1;j<=t;j++)
		{
			if(v[j]==0&&pri[k][j]<dis[j])
			dis[j]=pri[k][j];
		}
	}
}
int main()
{
	int i,j,n,m;
	double cas;
	while(scanf("%d",&t)!=EOF,t)
	{
		scanf("%d%d",&n,&m);
		for(i=1;i<=t;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].y);
		}
		for(i=1;i<=t;i++)
		{
			for(j=1;j<=t;j++)
			{
				double k=sqrt(1.0*((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)));
				pri[i][j]=pri[j][i]=k;
			}
		}
		cas=pri[n][m];
		pri[n][m]=pri[m][n]=0;//将规定两点之间的距离为0,这样不会影响后面的求和
		prime();
		printf("%.2lf\n",sum+cas);//最后加上两点的距离
    }
	return 0;
}


你可能感兴趣的:(HDOJ 4463 Outlets(最小生成树--prime))