HDOJ 4463 Outlets 最短路

Outlets

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


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<vector>
#include<cstdio>
#define MAXN 1005
#include<cmath>
using namespace std;
int m,n;
const double inf=1000000;
double point[MAXN][2];
vector<int>mmap[MAXN];
double val[MAXN][MAXN];
double vno[MAXN];
int vis[MAXN],t;
double  prim()
{
    double  re=0;int k=0;
    double  d[MAXN];
    fill(d,d+n+1,inf);
    fill(vis,vis+n+1,0);
    d[1]=0;
    while(true)
    {
        int v=-1;
        for(int i=1;i<=n;i++){
            if(!vis[i]&&(v==-1||d[i]<d[v]))
                v=i;
        }
        if(v==-1||d[v]==inf)
                return -1;
        re+=d[v];
        k++;
        if(k==n)return re;
        vis[v]=1;
        for(int i=0;i<mmap[v].size();i++)
        {
            int x=mmap[v][i];
            if(!vis[x]&&val[v][x]<d[x])
                d[x]=val[v][x];
        }

    }
}
int main()
{

    while(~scanf("%d",&n),n)
    {
        m=0;
        int p,q;
        scanf("%d%d",&p,&q);
        for(int i=1;i<=n;i++)
         {
             mmap[i].clear();
             scanf("%lf%lf",&point[i][0],&point[i][1]);

         }
         double s=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                m++;
                double x1=point[i][0],y1=point[i][1];
                double x2=point[j][0],y2=point[j][1];
                mmap[i].push_back(j);
                mmap[j].push_back(i);
                double len=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                val[i][j]=val[j][i]=len;
                if(i==p&&j==q||i==q&&j==p)
                {
                    s+=len;
                    val[i][j]=val[j][i]=0;
                }

            }
        }
        printf("%.2lf\n",s+prim());
    }
}
[ Copy to Clipboard ]    [ Save to File]

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-30 14:25  Thereisnospon  阅读(177)  评论(0编辑  收藏  举报