https://vjudge.ppsucxtt.cn/problem/POJ-2349ios
#include <iostream> #include <queue> #include <cstring> #include <algorithm> #include <cmath> #include <iomanip> #define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define debug(a) cout << "debug : " << (#a) << " = " << a << endl using namespace std; typedef long long ll; typedef pair<ll, ll> PII; const int N = 1010; const int INF = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 998244353; double a[N][N]; PII b[N]; vector<double> res; double dis[N]; bool vis[N]; int n, m; void prim() { for (int i = 0; i < N; i++) dis[i] = INF; memset(vis, false, sizeof vis); for (int i = 1; i <= n; i++) { int t = -1; for (int j = 1; j <= n; j++) { if (vis[j]) continue; if (t == -1 || dis[j] < dis[t]) t = j; } if (t != 1) res.push_back(dis[t]); vis[t] = true; for (int j = 1; j <= n; j++) dis[j] = min(dis[j], a[t][j]); } } int main() { fastio; int T; cin >> T; while (T--) { cin >> m >> n; for (int i = 1; i <= n; i++) cin >> b[i].first >> b[i].second; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) a[i][j] = a[j][i] = sqrt(pow(b[i].first - b[j].first, 2) + pow(b[i].second - b[j].second, 2)); prim(); sort(res.begin(), res.end()); int idx = res.size() - 1; if (idx - (m - 1) < 0) cout << 0 << endl; else cout << fixed << setprecision(2) << res[idx - (m - 1)] << endl; res.clear(); } return 0; }