蒟蒻最小生成树prim 42pts,求dalao解答
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int N = 1e3 + 10;
struct node {
double x, y;
};
int n;
node a[N];
bool vis[N];
double dis[N];
double ans;
void prim() {
for (int i = 1; i <= n; i++) dis[i] = 1e9;
priority_queue<pair<double, int>, vector<pair<double, int> >, greater<pair<double, int> > > pq;
pq.push({0, 1});
dis[1] = 0;
int cnt = 0;
while (!pq.empty()) {
if (cnt >= n) return;
int x = pq.top().second;
pq.pop();
if (vis[x]) continue;
vis[x] = true;
cnt++;
for (int y = 1; y <= n; y++) {
if (y == x) continue;
double d = sqrt((a[y].x - a[x].x) * (a[y].x - a[x].x) + (a[y].y - a[x].y) * (a[y].y - a[x].y));
if (d < dis[y]) {
dis[y] = d;
pq.push({d, y});
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].x >> a[i].y;
prim();
for (int i = 1; i <= n; i++)
ans = max(ans, dis[i]);
printf("%.7f\n", ans / 2);
return 0;
}