zoukankan      html  css  js  c++  java
  • Arctic Network POJ

    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #define x first
    #define y second
    using namespace std;
    typedef pair<int,int>PII;
    const int N=510,M=N*N/2;
    int n,k,m;
    struct edge
    {
        int a,b;
        double w;
    } e[M];
    double get_dist(PII a,PII b)
    {
        int dx=a.x-b.x;
        int dy=a.y-b.y;
        return sqrt(dx*dx+dy*dy);
    }
    bool cmp(edge a,edge b)
    {
        return a.w<b.w;
    }
    PII q[M];
    int p[N];
    int find(int x)
    {
        if(p[x]!=x)
            p[x]=find(p[x]);
        return p[x];
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            m=0;
            cin>>k>>n;
            for(int i=0; i<n; i++)
                cin>>q[i].x>>q[i].y;
            for(int i=0; i<n; i++)
                for(int j=0; j<i; j++)
                    e[m++]= {i,j,get_dist(q[i],q[j])};
            sort(e,e+m,cmp);
            for(int i=0; i<n; i++)
                p[i]=i;
            int cnt=n;
            double res=0;
            for(int i=0; i<m; i++)
            {
                if(cnt<=k)
                    break;
                int a=find(e[i].a);
                int b=find(e[i].b);
                double w=e[i].w;
                if(a!=b)
                {
                    p[a]=b;
                    cnt--;
                    res=w;
                }
            }
            //不能用lf 
            printf("%.2f
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    md转html,并带目录结构
    vue05
    vue04
    mysql索引及调优
    mysql的锁与事务
    python基本数据类型的操作
    redis集群
    docker 学习(四)
    MongoDB基本操作
    MongoDB基础
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12240203.html
Copyright © 2011-2022 走看看