並差集 ,結構體 暢通工程再續

                                        暢通工程再續

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 3   Accepted Submission(s) : 2

Problem Description

相信大家都聽說一個“百島湖”的地方吧,百島湖的居民生活在不同的小島中,當他們想去其他的小島時都要通過劃小船來實現。現在政府決定大力發展百島湖,發展首先要解決的問題當然是交通問題,政府決定實現百島湖的全暢通!經過考察小組RPRush對百島湖的情況充分了解後,決定在符合條件的小島間建上橋,所謂符合條件,就是2個小島之間的距離不能小於10米,也不能大於1000米。當然,爲了節省資金,只要求實現任意2個小島之間有路通即可。其中橋的價格爲 100元/米。

Input

輸入包括多組數據。輸入首先包括一個整數T(T <= 200),代表有T組數據。 每組數據首先是一個整數C(C <= 100),代表小島的個數,接下來是C組座標,代表每個小島的座標,這些座標都是 0 <= x, y <= 1000的整數。

Output

每組輸入數據輸出一行,代表建橋的最小花費,結果保留一位小數。如果無法實現工程以達到全部暢通,輸出”oh!”.

Sample Input

2

2

10 10

20 20

3

1 1

2 2

1000 1000

Sample Output

1414.2

oh!

Author

8600

Source

2008浙大研究生複試熱身賽(2)——全真模擬

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
    int x1,y1;
    double length;
} land[102*102];
int x[102],y[102];
int root[102];
int num[102];
bool cmp(node a,node b)
{
    return a.length<b.length;
}
int find_root(int son)
{
    if(root[son]!=son)
    {
        root[son]=find_root(root[son]);
    }
    return root[son];
}
int main()
{
    int times,c;
    scanf("%d",&times);
    while(times--)
    {
        scanf("%d",&c);
        for(int i=1; i<=c; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        for(int i=1; i<=c; i++)
        {
            num[i]=1;
            root[i]=i;
        }
        int n=1;
        for(int i=1; i<c; i++)
        {
            for(int j=i+1; j<=c; j++)
            {
                land[n].x1=i;
                land[n].y1=j;
                land[n].length=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                n++;
            }
        }
        sort(land+1,land+n,cmp);
        int fx,fy;
        double money=0;
        for(int i=1; i<n; i++)
        {
            if(land[i].length>=10&&land[i].length<=1000)
            {
                fx=find_root(land[i].x1);
                fy=find_root(land[i].y1);
                if(fx!=fy)
                {
                    money+=land[i].length*100;
                    root[fx]=fy;
                    num[fy]+=num[fx];
                }
                if(num[fy]>=c||num[fx]>=c)
                {
                    break;
                }
            }
        }
        if(num[fy]>=c||num[fx]>=c)
        {
            printf("%.1lf\n",money);
        }
        else
        {
            printf("oh!\n");
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章