|
| 1 | +// |
| 2 | +// main.cpp |
| 3 | +// V-Ball |
| 4 | +// |
| 5 | +// Created by Robert He on 2024/11/12. |
| 6 | +// |
| 7 | + |
| 8 | +#include <cstdio> |
| 9 | +#include <cstring> |
| 10 | +#include <iostream> |
| 11 | +using namespace std; |
| 12 | +using namespace chrono; |
| 13 | + |
| 14 | +const double Maxn = 1e9; |
| 15 | +const int N = 205, M = 8e4 + 5; |
| 16 | +const double eps = 1e-8; |
| 17 | +int pre[N], nxt[M], to[M], lst[N], fr[M], flw[M], Q[M]; |
| 18 | +int n, s, t, T; |
| 19 | +bool vis[N]; |
| 20 | +double a[N][N], b[N][N], cst[M], Ans, dis[N]; |
| 21 | + |
| 22 | +template <class T> |
| 23 | +inline void CkMin(T &a, const T b) { |
| 24 | + if (a > b) |
| 25 | + a = b; |
| 26 | +} |
| 27 | +inline void Add(const int x, const int y, const int z, const double g) { |
| 28 | + nxt[++T] = lst[x]; |
| 29 | + lst[x] = T; |
| 30 | + to[T] = y; |
| 31 | + fr[T] = x; |
| 32 | + flw[T] = z; |
| 33 | + cst[T] = g; |
| 34 | + nxt[++T] = lst[y]; |
| 35 | + lst[y] = T; |
| 36 | + to[T] = x; |
| 37 | + fr[T] = y; |
| 38 | + flw[T] = 0; |
| 39 | + cst[T] = -g; |
| 40 | +} |
| 41 | +inline bool Bul() { |
| 42 | + for (int i = s; i <= t; ++i) |
| 43 | + dis[i] = -Maxn; |
| 44 | + int te = 0, we = 1, x, y; |
| 45 | + dis[Q[1] = s] = 0.0; |
| 46 | + while (te < we) { |
| 47 | + x = Q[++te]; |
| 48 | + vis[x] = false; |
| 49 | + for (int i = lst[x]; i; i = nxt[i]) |
| 50 | + if (dis[y = to[i]] < dis[x] + cst[i] && flw[i]) { |
| 51 | + dis[y] = dis[x] + cst[i]; |
| 52 | + pre[y] = i; |
| 53 | + if (!vis[y]) { |
| 54 | + vis[y] = true; |
| 55 | + Q[++we] = y; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return dis[t] > -Maxn; |
| 60 | +} |
| 61 | +inline void Deal() { |
| 62 | + int Mif = Maxn; |
| 63 | + for (int i = pre[t]; i; i = pre[fr[i]]) |
| 64 | + CkMin(Mif, flw[i]); |
| 65 | + for (int i = pre[t]; i; i = pre[fr[i]]) { |
| 66 | + flw[i] -= Mif; |
| 67 | + flw[i ^ 1] += Mif; |
| 68 | + Ans += (double)Mif * cst[i]; |
| 69 | + } |
| 70 | +} |
| 71 | +inline bool check(const double mi) { |
| 72 | + T = 1; |
| 73 | + memset(lst, 0, sizeof(lst)); |
| 74 | + for (int i = 1; i <= n; ++i) |
| 75 | + Add(s, i, 1, 0), Add(i + n, t, 1, 0); |
| 76 | + for (int i = 1; i <= n; ++i) |
| 77 | + for (int j = 1; j <= n; ++j) |
| 78 | + Add(i, j + n, 1, a[i][j] - mi * b[i][j]); |
| 79 | + Ans = 0.0; |
| 80 | + while (Bul()) |
| 81 | + Deal(); |
| 82 | + return (Ans <= 0); |
| 83 | +} |
| 84 | + |
| 85 | +int main() { |
| 86 | + scanf("%d", &n); |
| 87 | + s = 0; |
| 88 | + t = (n << 1) + 1; |
| 89 | + |
| 90 | + for (int i = 1; i <= n; ++i) |
| 91 | + for (int j = 1; j <= n; ++j) |
| 92 | + scanf("%lf", &a[i][j]); |
| 93 | + |
| 94 | + for (int i = 1; i <= n; ++i) |
| 95 | + for (int j = 1; j <= n; ++j) |
| 96 | + scanf("%lf", &b[i][j]); |
| 97 | + |
| 98 | + double l = 0, r = 1e4; |
| 99 | + while (r - l >= eps) { |
| 100 | + double mid = (l + r) / 2; |
| 101 | + (check(mid) ? r : l) = mid; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + printf("%.6lf\n", l); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
0 commit comments