CodeForces - 434D Nanami's Power Plant

Description

一个长度为 \(n\) 的序列,序列上第 \(i\) 个数是 \(x_i\in [l_i,r_i]\) ,另外有 \(m\) 个条件,每个条件是一个三元组 \(<u,v,d>\) ,表示 \(x_u\le x_v+d\) 。令 \(\sum f_i(x_i)\) 最大,其中 \(f_i(x_i)=a_ix_i^2+b_ix+c_i\)

\(n\le 50,m\le 100,-100\le l_i,r_i\le 100)\)

Solution

听说这题和 bzoj 上切糕那题类似,没做过啊,我菜。

这题建图挺神仙的,每个数 \(x_i\) 拆成 \(r_i-l_i+2\) 个点,第 \(1\) 个点从 \(S\) 连一条流量 \(\inf\) 的边,第 \(r_i-l_i+2\) 个点向 \(T\) 连一条流量为 \(\inf\) 的边,对于第 \(j\in [2,r_i-l_i+1]\) 个点,连边 \((j,j+1,\inf - f_i(j+l_i))\) 的边。

对于限制 \(<u,v,d>\) ,对于 \(u\) 的第 \(j\in [2,r_u-l_u+1]\) 个点,向 \(v\) 的第 \(j-d\) 个点连 \(\inf\) 的边。

然后跑最小割,答案就是 \(\inf \times n - 最小割\)

这其实是一种“最大割”转最小割的操作。

#include<bits/stdc++.h>
using namespace std;

template <class T> void read(T &x) {
    x = 0; bool flag = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; flag ? x = ~x + 1 : 0;
}

#define N 10010
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define INF 0x3f3f3f3f

int head[N], cur[N], tot = 1, q[N], dep[N];
struct edge { int v, c, next; }e[200010];
inline void insert(int u, int v, int c) {
    e[++tot].v = v, e[tot].c = c, e[tot].next = head[u]; head[u] = tot;
}
inline void add(int u, int v, int c) { insert(u, v, c), insert(v, u, 0); }
inline bool bfs(int S, int T) {
    memset(dep, 0, sizeof dep); dep[S] = 1;
    int l = 1, r = 1; q[1] = S;
    while (l <= r) {
        int u = q[l++];
        for (int i = head[u], v; i; i = e[i].next) if (e[i].c && !dep[v = e[i].v]) {
            dep[v] = dep[u] + 1, q[++r] = v;
            if (v == T) return 1;
        }
    }
    return 0;
}
int dfs(int u, int dist, int T) {
    if (u == T) return dist;
    int ret = 0;
    for (int &i = head[u], v; i; i = e[i].next) if (dep[v = e[i].v] == dep[u] + 1 && e[i].c) {
        int d = dfs(v, min(dist - ret, e[i].c), T);
        e[i].c -= d, e[i ^ 1].c += d, ret += d;
        if (ret == dist) return dist;
    }
    if (!ret) dep[u] = -1;
    return ret;
}

int dinic(int S, int T) {
    int ret = 0; memcpy(cur, head, sizeof head);
    while (bfs(S, T)) ret += dfs(S, INF, T), memcpy(head, cur, sizeof cur);
    return ret;
}

int a[N], b[N], c[N], l[N], r[N], node[N];

inline int f(int x, int y) { return a[x] * y * y + b[x] * y + c[x]; }

inline int id(int x, int y) { return node[x] + y - l[x]; }

int main() {
    int n, m; read(n), read(m);
    rep(i, 1, n) read(a[i]), read(b[i]), read(c[i]);
    int cnt = 0, M = 0;
    rep(i, 1, n) {
        read(l[i]), read(r[i]);
        node[i] = cnt + 1, cnt += r[i] - l[i] + 2;
        rep(j, l[i], r[i]) M = max(M, f(i, j));
    }
    int S = cnt + 1, T = S + 1;
    rep(i, 1, n) {
        add(S, node[i], INF);
        rep(j, l[i], r[i]) add(id(i, j), id(i, j + 1), M - f(i, j));
        add(id(i, r[i] + 1), T, INF);
    }
    while (m--) {
        int u, v, d; read(u), read(v), read(d);
        rep(i, l[u], r[u]) if (i - d >= l[v] && i - d <= r[v] + 1) add(id(u, i), id(v, i - d), INF);
    }
    printf("%d", M * n - dinic(S, T));
    return 0;
}
posted @ 2018-09-06 19:41 aziint 阅读(...) 评论(...) 编辑 收藏
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.