最优答案一定是一颗树
那么二分比值,不断kruskal找到最大可以满足的解就可以了
代码如下
#include <cstdio> #include <algorithm> using namespace std; typedef double db; struct Node{ int a, b, c, d; db w; inline void init() { scanf("%d %d %d %d", &a, &b, &c, &d); } bool operator < (const Node &o) const{ return w < o.w; } }a[10010]; int n, m, f, fa[410]; int find(int x) { return (fa[x] == x) ? x : fa[x] = find(fa[x]); } inline bool kruskal(db x) { for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) a[i].w = (db)a[i].c + (db)a[i].d * x; sort(a + 1, a + m + 1); db ans = 0; int tot = 0; for (int i = 1; i <= m; i++) { int x = find(a[i].a); int y = find(a[i].b); if (x != y) { fa[x] = y; ans += a[i].w; tot++; } if (ans > f) return false; if (tot == n - 1) return true; } } int main() { scanf("%d %d %d", &n, &m, &f); for (int i = 1; i <= m; i++) a[i].init(); db l = 0, r = (db)f / (n - 1); while (r - l > 0.000001) { db mid = (l + r) / 2; if (kruskal(mid)) l = mid; else r = mid; } printf("%.4lf", l); return 0; }