HDU 1875(最小生成树)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef struct
{
    int a,b;
    double v;
}node;
typedef struct
{
    int a,b;
}P;
const int maxn=109;
double ans;
int father[maxn];
node graph[maxn*(maxn-1)/2];
P p[maxn];
int Find(int x)
{
    if(father[x]==x)
        return x;
    else
    {
        father[x]=Find(father[x]);
        return father[x];
    }
}
void Union(int x,int y,double v)
{
    if(Find(x)!=Find(y))
    {
        ans+=v*100;
        father[Find(x)]=Find(y);
    }
    return;
}
bool cmp(node x,node y)
{
    if(x.v<y.v)
        return true;
    else
        return false;
}
int main()
{
    double V;
    int n;
    scanf("%d",&n);
    while(n--)
    {
        ans=0;
        for(int i=0;i<maxn;i++)
            father[i]=i;
        int m,k;
        k=0;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&p[i].a,&p[i].b);
        }
        for(int i=0;i<m-1;i++)
        {
            for(int j=i+1;j<m;j++)
            {
                V=sqrt(pow((p[i].a-p[j].a),2.0)+pow((p[i].b-p[j].b),2.0));
                if(V<=1000&&V>=10)
                {
                    graph[k].a=i;
                    graph[k].b=j;
                    graph[k].v=V;
                    k++;
                }
            }
        }
        sort(graph,graph+k,cmp);
        for(int i=0;i<k;i++)
            Union(graph[i].a,graph[i].b,graph[i].v);
        int xx=Find(0);
        int flag;
        flag=0;
        for(int i=0;i<m;i++)
        {
            if(Find(i)!=xx)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            printf("oh!\n");
        else
            printf("%.1lf\n",ans);
    }
    return 0;
}


 

转载于:https://www.cnblogs.com/xiao-xue-di/p/9442735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值