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
| #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <numeric> using namespace std;
int n, m; const int N = 100002; int u[N], v[N], a[N], b[N];
double w[N]; int p[N];
int set[N];
int find_set(int x) { if (set[x] == x) return x; return set[x] = find_set(set[x]); }
bool union_set(int a, int b) { a = find_set(a); b = find_set(b); if (a == b) return false; set[a] = b; return true; }
int A, B;
bool check(double c) { for (int i = 0; i < m; i++) w[i] = a[i] - b[i] * c; sort(p, p + m, [](int i, int j){ return w[i] > w[j]; }); iota(set, set + n, 0); A = B = 0; for (int i = 0; i < m; i++) { const int e = p[i]; if (union_set(u[e], v[e])) { A += a[e]; B += b[e]; } } return A >= B * c; }
int gcd(int a, int b) { while (a && b) { if (a > b) a %= b; else b %= a; } return a | b; }
int main() { cin >> n >> m; for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &u[i], &v[i], &a[i], &b[i]); } iota(p, p + m, 0); double lo = 0, hi = 1e5; for (int t = 0; t < 100; t++) { double c = (hi + lo) * 0.5; if (check(c)) lo = c; else hi = c; } int g = gcd(A, B); printf("%d/%d\n", A/g, B/g); return 0; }
|