无线通讯网(先存)

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void read(int &n){
    int num=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        num=num*10+ch-'0';
        ch=getchar();
    }
    n=num*w;
}
const int maxn=1000005;
int s,p; 
struct point{int x,y;}po[maxn];
struct node{
    int u,v;double w;
    bool operator < (const node &x) const{return w<x.w;}
}e[maxn];
int newp=0,fa[maxn];
double getdist(point a,point b){return sqrt(pow((double)a.x-(double)b.x,2)+pow((double)a.y-(double)b.y,2));}//get distance
int dofind(int v){return fa[v]==v?v:fa[v]=dofind(fa[v]);}
void dounion(int v1,int v2){fa[dofind(v1)]=dofind(v2);}
double Kruskal(){
    for(int i=1;i<=p;i++) fa[i]=i;
    sort(e+1,e+1+newp);
    double cnt=0,value=0;
    for(int i=1;cnt<p-s;i++){
        if(dofind(e[i].u)!=dofind(e[i].v)){
            dounion(e[i].u,e[i].v);
            value=e[i].w;
            cnt++;
        }
    }
    return value;
}
void init(){
    read(s);read(p);
    for(int i=1;i<=p;i++){
        int x,y;read(x);read(y);
        po[++newp]=(point){x,y};
    }
    newp=0;
    for(int i=1;i<=p;i++)
        for(int j=i+1;j<=p;j++)
            e[++newp]=(node){i,j,getdist(po[i],po[j])};
}
int main(){
    init();
    printf("%.2lf",Kruskal());
    return 0;
}

转载于:https://www.cnblogs.com/saitoasuka/p/9989825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值