hdu_1875 畅通工程再续

#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
int n;
int Father[5005];
struct node
{
    int u,v;
    double w;
};
node edge[5005];
bool cmp(node a,node b)
{
    return a.w<b.w;
}
double dist(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt((x1-x2)*(x1-x2)+(y2-y1)*(y2-y1));
    return d;
}

void MakeSet()
{
    for(int i=1; i<=n; i++)
        Father[i]=i;
}
int Find(int x)
{
    while(x!=Father[x])
        x=Father[x];
    return x;
}
void Union(int x,int y)
{
    int a=Find(x);
    int b=Find(y);
    if(a!=b)
        Father[a]=b;
}
int main()
{
    double a[5005],b[5005];
    int t,T,i,j,k,p,x,y,z;
    double sum;
    cin>>T;
    while(T--)
    {
        cin>>t;
        for(i=1; i<=t; i++)
            cin>>a[i]>>b[i];
        n=t*(t-1)/2;
        x=0,y=0,z=0;
        sum=0,p=0;
        MakeSet();
        for(i=1; i<=t; i++)
            for(j=i+1; j<=t; j++)
            {
                if(dist(a[i],b[i],a[j],b[j])>=10&&dist(a[i],b[i],a[j],b[j])<=1000)
                {
                    edge[x++].u=i;
                    edge[y++].v=j;
                    edge[z++].w=dist(a[i],b[i],a[j],b[j]);
                }
            }
        sort(edge,edge+n,cmp);
        for(k=0; k<n; k++)
            if(Find(edge[k].u)!=Find(edge[k].v))
            {
                sum=sum+edge[k].w;
                Union(edge[k].u,edge[k].v);
            }
        for(k=1; k<=n; k++)
            if(Father[k]!=k)
                p++;
        if(p==t-1)
            cout<<setiosflags(ios::fixed)<<setprecision(1)<<sum*100<<endl;
        else
            cout<<"oh!"<<endl;
    }
    return 0;
}

你可能感兴趣的:(Algorithm,HDU,Spanning,Tre,Minimun)