zoukankan      html  css  js  c++  java
  • AcWing1145 北极通讯网络

    这题具有单调性质,可以二分,但是我们发现如果使用并查集维护kruscal,那么无需二分,直接枚举答案即可

    #include<bits/stdc++.h>
    #define x first
    #define y second
    using namespace std;
    typedef pair<int,int> pll;
    const int N=510000;
    struct node{
        int a,b;
        double c;
        bool operator <(const node &t) const{
            return c<t.c;
        }
    }e[N];
    pll q[N];
    int p[N];
    double get(pll a,pll b){
        int x=a.x-b.x;
        int y=a.y-b.y;
        return sqrt(x*x+y*y);
    }
    int find(int x){
        if(x!=p[x]){
            p[x]=find(p[x]);
        }
        return p[x];
    }
    int main(){
        int n,k;
        int i,j;
        cin>>n>>k;
        for(i=1;i<=n;i++){
            cin>>q[i].x>>q[i].y;
        }
        for(i=1;i<=n;i++)
        p[i]=i;
        int cnt=0;
        for(i=1;i<=n;i++){
            for(j=1;j<i;j++){
                e[cnt++]=node{i,j,get(q[i],q[j])};
            }
        }
        sort(e,e+cnt);
        double res=0;
        int tmp=n;
        for(i=0;i<cnt;i++){
            if(tmp<=k)
            break;
            int pa=find(e[i].a),pb=find(e[i].b);
            if(pa!=pb){
                p[pa]=pb;
                tmp--;
                res=e[i].c;
            }
        }
        printf("%.2f
    ",res);
    }
    View Code
  • 相关阅读:
    Bzoj3339 Rmq Problem
    Bzoj3509 [CodeChef] COUNTARI
    浅析python日志重复输出问题
    mysql练习题
    python学习之思维导图
    python面向对象编程练习
    Python常见下划线
    内置方法
    类的绑定方法与非绑定方法
    封装
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12838290.html
Copyright © 2011-2022 走看看