P1991-无线通讯网【最小生成树,瓶颈生成树】

版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/87820402

正题


题目大意

n n 个点,连边长度不超过 D D 的情况下分为 S S 个联通块。
求最小的 D D


解题思路

直接 K r u s k a l Kruskal 连边连到只剩下 S S 个联通块就好了。


c o d e code

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=510,M=250000;
struct node{
	int x,y;
	double w;
}a[M];
int n,m,fa[N],k,s;
double x[N],y[N];
double ans;
bool cmp(node x,node y)
{return x.w<y.w;}
int find(int x)
{return fa[x]==x?x:find(fa[x]);}
double count_dis(int a,int b)
{return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));}
int main()
{
	scanf("%d%d",&s,&n);
	for(int i=1;i<=n;i++)
		scanf("%lf%lf",&x[i],&y[i]);
	for(int i=1;i<n;i++)
	  for(int j=i+1;j<=n;j++){
	  	a[++m].x=i;a[m].y=j;
	  	a[m].w=count_dis(i,j);
	  }
	for(int i=1;i<=n;i++)
	  fa[i]=i;
	k=n;
	sort(a+1,a+1+m,cmp);
	for(int i=1;i<=m;i++){
		int Fa=find(a[i].x),Fb=find(a[i].y);
		if(Fa!=Fb){
			fa[Fb]=Fa;
			ans=a[i].w;
			k--;
		}
		if(k==s) break;
	}
	printf("%.2lf",ans);
}

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/87820402