码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 4463 Outlets 最小生成树

时间:2015-08-17 23:47:31      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:


Prim....似乎没有考虑多点共线也能A.....

Outlets

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


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
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年08月17日 星期一 21时13分29秒
File Name     :HDOJ4463.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const double inf=1e30;
const int maxn=100;

int n;
double g[maxn][maxn];
double dist[maxn];
bool vis[maxn];

double prim()
{
	for(int i=0;i<100;i++) { dist[i]=inf; vis[i]=false; }
	dist[0]=0;
	double sum=0;
	for(int i=0;i<n;i++)
	{
		int mark=-1;
		double mindist=inf;
		for(int j=0;j<n;j++)
		{
			if(!vis[j]&&dist[j]<mindist)
			{
				mark=j; mindist=dist[j];
			}
		}
		if(mark==-1) return inf;
		sum+=mindist; vis[mark]=true;
		for(int j=0;j<n;j++)
		{
			if(!vis[j]&&g[mark][j]<dist[j])
			{
				dist[j]=g[mark][j];
			}
		}
	}
	return sum;
}

int P,Q,N;

struct Point
{
	double x,y;
	void toString()
	{
		printf("(%lf,%lf)\n",x,y);
	}
}pt[maxn],sP,sQ;

double Dist(Point a,Point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d",&N)!=EOF&&N)
	{
		scanf("%d%d",&P,&Q);
		P--; Q--; n=0;
		for(int i=0,x,y;i<N;i++)
		{
			scanf("%d%d",&x,&y);
			if(i==P) sP.x=x,sP.y=y;
			else if(i==Q) sQ.x=x,sQ.y=y;
			else pt[n].x=x,pt[n].y=y,n++;
		}

		n++; 
		for(int i=0;i<n-1;i++)
		{
			g[i][n-1]=g[n-1][i]=min(Dist(pt[i],sP),Dist(pt[i],sQ));
		}

		for(int i=0;i<n-1;i++)
		{
			for(int j=0;j<n-1;j++)
			{
				g[i][j]=g[i][j]=Dist(pt[i],pt[j]);
			}
		}

		double ans=prim()+Dist(sP,sQ);
		printf("%.2lf\n",ans);
	}
    
    return 0;
}




版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss

HDOJ 4463 Outlets 最小生成树

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47733285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!