zoukankan      html  css  js  c++  java
  • 畅通工程再续 HDU

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define x first
    #define y second
    const int N=210;
    typedef pair<double,double>PII;
    PII a[N];
    int p[N];
    struct edge{
        int a,b;
        double w;
    }e[N*N];
    bool cmp(edge a,edge b)
    {
        return a.w<b.w;
    }
    double get_dist(int i,int j)
    {
        int dx=a[i].x-a[j].x;
        int dy=a[i].y-a[j].y;
        return sqrt(dx*dx+dy*dy);
    }
    int find(int x)
    {
        if(p[x]!=x)
            p[x]=find(p[x]);
        return p[x];
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            memset(p,0,sizeof p);
            memset(e,0,sizeof e);
            memset(a,0,sizeof a);
            int n;
            cin>>n;
            for(int i=1;i<=n;i++)
                cin>>a[i].x>>a[i].y;
            for(int i=1;i<=n;i++)
                p[i]=i;
            int k=0;
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++)
                    {
                        double w=get_dist(i,j);
                        if(w>=10&&w<=1000)
                            e[k++]={i,j,w};
                    }
            sort(e,e+k,cmp);
            double sum=0;
            for(int i=0;i<k;i++)
            {
                int a=find(e[i].a);
                int b=find(e[i].b);
                double w=e[i].w;
                if(a!=b)
                {
                    p[a]=b;
                    sum+=w;
                }
            }
            int ans=0;
            for(int i=1;i<=n;i++)
                if(p[i]==p[1])
                    ans++;
            if(ans==n)
                printf("%.1f
    ",sum*100);
            else
                cout<<"oh!"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud云架构
    Spring Cloud Consul
    Spring Cloud Eureka
    构建Spring Cloud微服务分布式云架构
    数据库三范式
    redis3.0.0 集群安装详细步骤
    sql优化的几种方法
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12240763.html
Copyright © 2011-2022 走看看