求助!50!WA!
查看原帖
求助!50!WA!
696431
lijunxi1楼主2023/7/7 19:53

贪心+并查集:

#include<bits/stdc++.h>
using namespace std;
int n, m, f[1005], c[1005], d[1005];
int cf(int x) {
	if (f[x] == 0)return x;
	return f[x] = cf(f[x]);
}
struct aa {
	int bh1, bh2;
	double jl;
	bool operator<(const aa& a)const {
		return jl > a.jl;
	}
} a;
priority_queue<aa>q;
int main ( ) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d%d", &c[i], &d[i]);
		a.bh1 = i;
		for (int j = 1; j < i; j++) {
			a.jl = sqrt((c[i] - c[j]) * (c[i] - c[j]) + (d[i] - d[j]) * (d[i] - d[j])), a.bh2 = j;
			q.push(a);
		}
	}
	for (int i = 1; i <= n - m; i++) {
		while (cf(q.top().bh1) == cf(q.top().bh2))q.pop();
		a = q.top();
		q.pop();
		if (cf(a.bh1) > cf(a.bh2))f[cf(a.bh1)] = cf(a.bh2);
		else f[cf(a.bh2)] = cf(a.bh1);
	}
	printf("%.2lf", q.top().jl);
}
2023/7/7 19:53
加载中...