zoukankan      html  css  js  c++  java
  • 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!
    ");
            else
                printf("%.1lf
    ",ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    hdu 6049 Sdjpx Is Happy
    L2-012. 关于堆的判断
    L2-010. 排座位
    L2-009. 抢红包
    L2-007. 家庭房产
    L2-008. 最长对称子串
    L2-011. 玩转二叉树
    l2-006 树的遍历
    l2-005
    l1-20 帅到没朋友
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/9442735.html
Copyright © 2011-2022 走看看