#8520. 贪吃的老鼠 (cheese)题解

C++:

#include <algorithm>
#include <cstdio>
#include <cstring>
#define N 2000
#define M 110000
using namespace std;
int A[N], B[N], C[N], D[N], S, T, X, a[N], b[M], c[M], e[N], h[N], m, n, q[N], s, t;
double E[N], d[M];
inline void add(int u, int v, double w) {
    b[++t] = a[u], c[a[u] = t] = v, d[t] = w, b[++t] = a[v], c[a[v] = t] = u, d[t] = 0;
}
double dfs(int u, double f) {
    if (u == X)
        return f;
    double v = 0, x;
    for (int& i = e[u]; i; i = b[i])
        if (d[i] && h[c[i]] == h[u] + 1 && (x = dfs(c[i], min(d[i], f)))) {
            f -= x, v += x, d[i] -= x, d[i ^ 1] += x;
            if (!f)
                return v;
        }
    h[u] = -1;
    return v;
}
inline bool bfs() {
    memset(h + 1, -1, X << 2), *h = 0;
    for (int l = 0, r = 1; l < r;)
        for (int u = q[l++], i = a[u]; i; i = b[i])
            if (d[i] && h[c[i]] < 0)
                h[q[r++] = c[i]] = h[u] + 1;
    return h[X] >= 0;
}
inline bool dinic() {
    while (bfs()) memcpy(e, a, X + 1 << 2), dfs(0, 1e18);
    for (int i = *a; i; i = b[i])
        if (d[i])
            return false;
    return true;
}
int main() {
    freopen("cheese.in", "r", stdin), freopen("cheese.out", "w", stdout);
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &m, &n);
        double l = 0, o, r = 3000000;
        for (int i = 1; i <= m; i++) scanf("%d%d%d", A + i, B + i, C + i);
        for (int i = 1; i <= n; i++) scanf("%d", D + i);
        sort(D + 1, D + n + 1), reverse(D + 1, D + n + 1), D[n + 1] = 0;
        for (int i = 1; i <= n; i++) D[i] -= D[i + 1];
        while (r - l >= 1e-8) {
            o = (l + r) / 2, S = 0, t = 1;
            for (int i = 1; i <= m; i++) E[++S] = B[i], E[++S] = C[i] + o, add(0, i, A[i]);
            sort(E + 1, E + S + 1), s = 1;
            for (int i = 2; i <= S; i++)
                if (E[i] != E[s])
                    E[++s] = E[i];
            S = m, X = (s - 1) * n + m + 1;
            for (int i = 1; i < s; S += n, i++) {
                for (int j = 1; j <= n; j++) add(S + j, X, (E[i + 1] - E[i]) * D[j] * j);
                for (int j = 1; j <= m; j++)
                    if (E[i] - B[j] >= -1e-8 && C[j] + o - E[i + 1] >= -1e-8)
                        for (int k = 1; k <= n; k++) add(j, S + k, (E[i + 1] - E[i]) * D[k]);
            }
            dinic() ? r = o : l = o, memset(a, 0, X + 1 << 2);
        }
        printf("%.8lf\n", l);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值