【LOJ6008】【网络流24题】餐巾计划

Description

https://loj.ac/problem/6008


Solution

很巧妙的建图哇~

我们将每一天拆成两个点 i i
S i 连边,流量为 r i ,费用为 0 ,表示会产生 r i 块旧餐巾。
i T 连边,流量为 r i ,费用为 0 ,表示需要 r i 块新餐巾。
i i + 1 连边,流量为 + ,费用为 0 ,表示将旧餐巾留着以后洗。
S i 连边,流量为 + ,费用为 p ,表示直接买新餐巾。
i ( i + n ) ( i + m ) 连边,流量为 + ,费用为 f s ,表示洗餐巾。

跑费用流即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jul 12th, 2018
 * Prob: LOJ6008
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 4005, maxm = maxn * maxn;

int n, P, N, S, F, M, s, t, beg[maxn], v[maxm], nex[maxm], f[maxm], w[maxm], vis[maxn], e = 1, dis[maxn], r[maxn];
LL Cost, Flow;

inline void add(int uu, int vv, int ff, int ww, int fl = 1) {
    v[++ e] = vv, f[e] = ff, w[e] = ww, nex[e] = beg[uu], beg[uu] = e;
    if (fl) add(vv, uu, 0, -ww, 0);
}

inline bool BFS()
{
    static queue<int> q;
    For(i, 1, t) dis[i] = INF;
    Set(vis, 0), dis[s] = 0, q.push(s);
    while (!q.empty()) {
        int u = q.front(); q.pop(), vis[u] = 0;
        for (register int i = beg[u]; i; i = nex[i])
            if (f[i] && chkmin(dis[v[i]], dis[u] + w[i]))
                if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);
    }
    return dis[t] != INF;
}

int DFS(int u, int flow)
{
    if (u == t) return flow;
    int res = flow, tmp;
    vis[u] = 1;
    for (register int i = beg[u]; i; i = nex[i])
        if (f[i] && !vis[v[i]] && dis[v[i]] == dis[u] + w[i]) {
            tmp = DFS(v[i], min(res, f[i]));
            f[i] -= tmp, f[i ^ 1] += tmp, Cost += tmp * (LL)w[i];
            if (!(res -= tmp)) return flow;
        }
    return flow - res;
}

int main()
{
#ifdef hany01
    File("loj6008");
#endif

    n = read()/*, P = read(), M = read(), F = read(), N = read(), S = read()*/, t = (s = (n << 1) + 1) + 1;
    For(i, 1, n) r[i] = read();
    P = read(), M = read(), F = read(), N = read(), S = read();
    For(i, 1, n) {
        int tmp = r[i];
        add(s, i, tmp, 0), add(i + n, t, tmp, 0), add(s, i + n, INF, P);
        if (i != n) add(i, i + 1, INF, 0);
        if (i + M <= n) add(i, i + n + M, INF, F);
        if (i + N <= n) add(i, i + n + N, INF, S);
    }

    while (BFS()) do Set(vis, 0), Flow += DFS(s, INF); while (vis[t]);
    printf("%lld\n", Cost);

    return 0;
}
//三分春色二分愁,更一分风雨。
//    -- 叶清臣《贺圣朝·留别》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81017975