hdu 4463,mst

K. Outlets

1000ms
1000ms
32768KB
64-bit integer IO format: %I64d      Java class name: Main
Font Size:
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<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXM 2222
#define MAXN 55
int u[MAXM],v[MAXM],n;
double w[MAXM];
int p,q,r[MAXM],f[MAXN];

struct node{
    int x,y;
}pt[MAXN];

bool cmp(int a, int b){
    return w[a]<w[b];
}

int fa(int x){
    if(x == f[x]) return x;
    else return f[x] = fa(f[x]);
}

double kral(double &ans){
    int i,j;
    for(i=0; i<n; i++) f[i]=i;
    f[p-1]=f[q-1]=q-1;
    for(j=0,i=0; ; i++){
        int x=fa(u[r[i]]), y=fa(v[r[i]]);
        if(x!=y){
            f[x]=y;
            ans+=w[r[i]];
            if(++j==n-2) break;
        }
    }
    return ans;
}

int main(){
//  freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
    while(scanf(" %d",&n)==1 &&n){
        int i,j;
        scanf(" %d %d",&p,&q);
        for(i=0; i<n; i++) scanf(" %d %d",&pt[i].x,&pt[i].y);

        int e=0;
        double ans=0;
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++){
                w[e]=sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)
                         +(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y));
                u[e]=i; v[e]=j;
                if(i+1==p && j+1==q) ans+=w[e];
                e++;
            }

        for(i=0; i<e; i++) r[i]=i;
        sort(r,r+e,cmp);
/*
        for(i=0; i<e; i++)
            printf("%d %f
",r[i],w[r[i]]);
        printf("
");
 */       printf("%.2f
",kral(ans));
    }

    return 0;
}
原文地址:https://www.cnblogs.com/ramanujan/p/3412914.html