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

hdu 4463 Outlets(最小生成树)

时间:2016-07-31 17:29:00      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

Outlets

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 39   Accepted Submission(s) : 26

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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<climits>
#include<cmath>
using namespace std;

double sum;
int i,j,n;
bool vis[55];
int x[55],y[55];
double dis[55],mp[55][55];
void prim()
{
    for(int i=1;i<n-1;i++)
    {
        double minn=INT_MAX*1.0;
        int k;
        for(int j=1;j<=n;j++)
          if (!vis[j] && dis[j]<minn)
          {
              k=j;
              minn=dis[j];
          }
        vis[k]=1;
        sum+=minn;
        for(int j=1;j<=n;j++)
        if (!vis[j] && mp[k][j]<dis[j]) dis[j]=mp[k][j];
    }
    return;
}
int main()
{
    while(scanf("%d",&n) && n)
    {
        int u,v;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&u,&v);
        for(i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);
        for(i=1;i<=n;i++)
            for(j=i+1;j<=n;j++)
        {
            double d=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
            mp[i][j]=d;
            mp[j][i]=d;
        }
       sum=mp[u][v];//先把那两家店连起来
       vis[u]=1;
       vis[v]=1; //全部标记走过
       for(i=1;i<=n;i++)  dis[i]=mp[u][i];
       for(i=1;i<=n;i++)  dis[i]=min(dis[i],mp[v][i]);//预处理到其他点的距离
       prim();
       printf("%.2lf\n",sum);
    }
    return 0;
}

 

hdu 4463 Outlets(最小生成树)

标签:

原文地址:http://www.cnblogs.com/stepping/p/5723152.html

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