有 n 座城市,已知坐標和人口。需修路連通所有城市,使總花費最小(最小生成樹)。對 MST 上每條邊 (u,v),考慮刪除它並用另一條邊 (x,y) 替換,使 (population[x]+population[y]) / (MST總權重 - w(u,v)) 最大。求該最大比值。
mst。A/B,其中 A 為兩人口最大城市人口和,B 為 mst - w(u,v)。取最大值。#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1010;
struct Point { int x, y; } city[maxn];
int pop[maxn], n;
struct Edge { int u, v; double w; };
vector mstEdges;
double mstWeight;
int parent[maxn];
bool vis[maxn];
double ans = 0;
// Prim 求 MST
void prim() { ... }
// DFS 求子樹最大人口城市
int dfs(int u, int p) {
int best = u;
for (auto e : tree[u]) if (e.v != p) {
int cand = dfs(e.v, u);
if (pop[cand] > pop[best]) best = cand;
}
maxPop[u] = best;
return best;
}
// 主程序枚舉 MST 邊計算最大比值
int main() {
while (cin >> n) {
// 讀入數據、構建完全圖、Prim、DFS、枚舉邊...
printf("%.2f\n", ans);
}
}