1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include<bits/stdc++.h> #define N 1001 #define M 2002 using namespace std; int T, n, x[N], y[N], p[N]; bool st[N]; double g[N][N], d[N], w[M]; int h[N], e[M], ne[M], tot; int path[N]; vector<pair<int, int>> vp; void add(int u, int v, int ww) { e[tot] = v, w[tot] = ww, ne[tot] = h[u], h[u] = tot++; } double prim() { double res = 0; for (int i = 0; i < n; ++i) d[i] = 0x3f3f3f3f, st[i] = 0; d[0] = 0; for (int i = 0; i < n; ++i) { int t = -1; for (int j = 0; j < n; ++j) if (!st[j] && (t == -1 || d[j] < d[t])) t = j; st[t] = 1; if (i) { res += d[t]; add(path[t], t, g[path[t]][t]); add(t, path[t], g[path[t]][t]); vp.push_back({t, path[t]}); } for (int j = 0; j < n; ++j) if (!st[j] && d[j] > g[t][j]) d[j] = g[t][j], path[j] = t; } return res; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); memset(h, -1, sizeof h); tot = 0; vp.clear(); for (int i = 0; i < n; ++i) scanf("%d%d%d", x + i, y + i, p + i); for (int i = 0; i < n; ++i) { g[i][i] = 0; for (int j = i + 1; j < n; ++j) g[i][j] = g[j][i] = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)); } double mst = prim(), ans = 0; for (auto &[u, v] : vp) { int p1 = 0, p2 = 0; function<void(int, int, int&)> dfs = [&](int f, int u, int& pv) { pv = max(pv, p[u]); for (int i = h[u]; ~i; i = ne[i]) if (e[i] != f) dfs(u, e[i], pv); }; dfs(u, v, p1), dfs(v, u, p2); ans = max(ans, (p1 + p2) / (mst - g[u][v])); } printf("%.2lf\n", ans); } return 0; }
|