hdu 4463 Outlets(最小生成树)

来源:互联网 发布:淘宝网店在哪里看 编辑:程序博客网 时间:2024/06/05 09:25

Outlets

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


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
42 30 01 00 -1 1 -10
 

Sample Output
3.41
 

Source
2012 Asia Hangzhou Regional Contest
 

Recommend
We have carefully selected several similar problems for you:  5193 5192 5190 5189 5188 
 


题意:

有n家店,要你把他们连在一起(即建成一颗最小生成树),q,p是要求必须有边直接连的,然后就给你n个店的坐标。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 110#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos ( -1.0 );typedef long long ll;const int INF = 1000010;using namespace std;int n;int A,B;int par[N],R[N];double X[N],Y[N];struct edge {    int u,v;    double cost;};edge es[N*N];int E;void init() {    for(int i=0; i<=n; i++) {        par[i]=i;        R[i]=0;    }}int finds(int x) {    if(par[x]==x)return x;    return par[x]=finds(par[x]);}void unite(int x,int y) {    x=finds(x);    y=finds(y);    if(x==y)return;    if(R[x]<R[y])par[x]=y;    else {        par[y]=x;        if(R[x]==R[y])R[x]++;    }}bool same(int x,int y) {    return finds(x)==finds(y);}double dis(int i,int j) {    return sqrt((X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]));}bool cmp(edge a,edge b) {    return a.cost<b.cost;}void kruscal() {    double ans=dis(A,B);    unite(A,B);    for(int i=0; i<E; i++) {        edge e=es[i];        if(!same(e.u,e.v)) {            ans+=e.cost;            unite(e.u,e.v);        }    }    printf("%.2f\n",ans);}int main() {    while(cin>>n&&n) {        cin>>A>>B;        for(int i=1; i<=n; i++)            cin>>X[i]>>Y[i];        E=0;        for(int i=1; i<=n; i++) {            for(int j=i+1; j<=n; j++) {                es[E].u=i;                es[E].v=j;                es[E++].cost=dis(i,j);            }        }        init();        sort(es,es+E,cmp);        kruscal();    }    return 0;}


0 0