1408090823-hd-畅通工程再续.cpp

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct zb
{
    int x,y;
}zbs[110];
int bridge[110];
bool cmp1(zb a,zb b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    else
        return a.y<b.y;
}
struct road
{
    int x,y;
    double l;
}roads[5000];
bool cmp2(road a,road b)
{
    return a.l<b.l;
}
int find(int a)
{
    if(bridge[a]==a)
        return a;
    else
        return bridge[a]=find(bridge[a]);
}
int main()
{
    int t,n;
    int i,j,k,l,num;
    double sum;
    double c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            bridge[i]=i;
        for(i=1;i<=n;i++)
            scanf("%d%d",&zbs[i].x,&zbs[i].y);
        sort(zbs+1,zbs+1+n,cmp1);
        l=0;
       // for(i=0;i<n*(n-1)/2;i++)
            for(j=1;j<=n;j++)
                for(k=j+1;k<=n;k++)
                {
                    c=sqrt(pow((zbs[k].x-zbs[j].x),2)+pow((zbs[k].y-zbs[j].y),2));
                    if(c>=10.0&&c<=1000.0)
                    //在这儿就判断一下,如果不符合条件直接就不存入结构体
                    {
                        roads[l].x=j;
                        roads[l].y=k;
                        roads[l].l=c;
                        l++;
                    }
                }
        sort(roads,roads+l,cmp2);
        sum=0;
        for(i=0;i<l;i++)
        {
            j=find(roads[i].x);
            k=find(roads[i].y);
            if(j!=k)
            {
                bridge[j]=k;
                sum+=roads[i].l;
            }
        }
        num=0;
        for(i=1;i<=n;i++)
            if(bridge[i]==i)
                num++;
        if(num==1)
            printf("%.1lf\n",sum*100);
        else
            printf("oh!\n");
    }
    return 0;
}

你可能感兴趣的:(1408090823-hd-畅通工程再续.cpp)