HDU 4463 Outlets 第37届ACM/ICPC 杭州赛区K题 (裸最小生成树)

Outlets

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

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
2012 Asia Hangzhou Regional Contest    
很简单的最小生成树了。
就是有一条边必须先加进去。  

//============================================================================
// Name        : HDU4463.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=100;
int F[MAXN];

struct Edge
{
    int from,to;
    double w;
};
struct point
{
    double x,y;
};
point P[MAXN];

Edge edge[MAXN*MAXN];

bool cmp(Edge a,Edge b)
{
    return a.w<b.w;
}

int find(int x)
{
    if(F[x]==-1)return x;
    else return F[x]=find(F[x]);
}
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double ans;
int p,q;

void solve(int n)
{
    int tol=0;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            edge[tol].from=i;
            edge[tol].to=j;
            edge[tol].w=dis(P[i],P[j]);
            if((i==p&&j==q)||(i==q&&j==p))
                edge[tol].w=0;
            tol++;
        }
    ans=dis(P[p],P[q]);
    sort(edge,edge+tol,cmp);
    memset(F,-1,sizeof(F));
    int cnt=0;
    for(int i=0;i<tol;i++)
    {
        int u=edge[i].from;
        int v=edge[i].to;
        int t1=find(u);
        int t2=find(v);
        if(t1!=t2)
        {
            ans+=edge[i].w;
            F[t1]=t2;cnt++;
            if(cnt==n-1)break;
        }
    }

}

int main() {
    int n;
    while(scanf("%d",&n)==1 && n)
    {
        scanf("%d%d",&p,&q);
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&P[i].x,&P[i].y);
        solve(n);
        printf("%.2lf\n",ans);
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/11/08/2761081.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞