求查错
查看原帖
求查错
222104
_yjh楼主2020/10/16 21:40
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,k,cnt,x[1005],y[1005],f[1005],p;
double ans,r[1005];
struct Edge {
	int x,y;
	double q;
}e[499501];
bool operator < (Edge a,Edge b) {
	return a.q<b.q;
}
int Find(int x) {
	if(x==f[x]) {
		return x;
	}
	return f[x]=Find(f[x]);
}
void Merge(int x,int y) {
	int fx=Find(x),fy=Find(y);
	if(fx!=fy) {
		f[fx]=fy;
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;i++) {
		cin>>x[i]>>y[i];
		for(int j=1;j<i;j++) {
			e[++cnt].x=i;
			e[cnt].y=j;
			e[cnt].q=(double)sqrt((e[i].x-e[j].x)*(e[i].x-e[j].x)+(e[i].y-e[j].y)*(e[i].y-e[j].y));
		}
		f[i]=i;
	}
	sort(e+1,e+n+1);
	cnt=0;
	while(p<n-1) {
		++cnt;
		if(Find(e[cnt].x)!=Find(e[cnt].y)) {
			Merge(e[cnt].x,e[cnt].y);
			r[++p]=e[cnt].q;
		}
	}
	printf("%.2lf",r[n-k+1]); 
	return 0;
}
2020/10/16 21:40
加载中...