Hdoj1875暢通工程再續

Hdoj1875:

暢通工程再續

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
 

Recommend
lcy
 

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
    int x,y;
    double cost;
    bool operator<(const node &q)const{
        return cost<q.cost;
    }
}a[5000];
int n,p[101],cot,q[101][2];
double ans;
int fid(int x)
{
    return p[x]==x?x:fid(p[x]);
}
void join(int x,int y,int c)
{
    int f1=fid(x);
    int f2=fid(y);
    if(f1!=f2)
    {
        cot--;
        ans+=a[c].cost;
        p[f1]=f2;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0.0;
        scanf("%d",&n);
        cot=n-1;
        for(int i=0;i<n;i++)
            p[i]=i;
        for(int i=0;i<n;i++)
            scanf("%d %d",&q[i][0],&q[i][1]);
        int k=0;
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
            {
                int tmp=(q[i][0]-q[j][0])*(q[i][0]-q[j][0])+(q[i][1]-q[j][1])*(q[i][1]-q[j][1]);
                if(tmp>=100&&tmp<=1000000)
                {
                    a[k].x=i;
                    a[k].y=j;
                    a[k++].cost=sqrt((double)tmp);
                }
            }
        sort(a,a+k);
        for(int i=0;i<k;i++)
            join(a[i].x,a[i].y,i);
        if(cot)
            printf("oh!\n");
        else
            printf("%.1lf\n",ans*100);
    }
    return 0;
}


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