• 无线通讯网(先存)


    #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;
    }
    
  • 相关阅读:
    jquery判断元素是否可见隐藏
    jQuery的replaceWith()函数用法详解
    前端工作面试问题
    Windows下安装sass和compass失败的解决办法
    马尾图案之canvas的translate、scale、rotate的方法详解
    boost bimap
    boost multi index
    boost regex expression
    boost format
    boost lexical_cast
  • 原文地址:https://www.cnblogs.com/saitoasuka/p/9989825.html
Copyright © 2020-2023  润新知