40分求助,感觉没问题呀
查看原帖
40分求助,感觉没问题呀
231769
忘れ潮楼主2020/9/7 13:41
#include <bits/stdc++.h>
using namespace std;
int n, k, p, cnt, xx[505], yy[505], f[505];
double ans;
struct node{
	int x, y; double dis;
}a[304800];
bool cmp(node c, node d){
	return c.dis < d.dis;
}
int fa(int x){
	if(x == f[x]) return x;
	f[x] = fa(f[x]);
	return f[x];
}
int main(){
	scanf("%d%d", &k, &n);
	for(int i = 1; i <= n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	if(k >= n){
		printf("0.00");
		return 0;
	}
	if(k == 0) k = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			if(i != j){
				a[++cnt].x = i;
				a[cnt].y = j;
				a[cnt].dis = 1.0000 * sqrt((xx[i] - xx[j]) * (xx[i] - xx[j]) + (yy[i] - yy[j]) * (yy[i] - yy[j]));
			}
	sort(a + 1, a + cnt + 1, cmp);
	for(int i = 1; i <= n; i++)
		f[i] = i;
	p = n;
	for(int i = 1; i <= cnt; i++)
		if(fa(a[i].x) != fa(a[i].y)){
			f[a[i].x] = a[i].y;
			p--;
			if(p == k){
				ans = a[i].dis;
				break;
			}
		}
	printf("%.2f", ans);
	return 0;
}
2020/9/7 13:41
加载中...