LOJ #6008. 「网络流 24 题」餐巾计划

你运营一家餐厅.你预测未来 n n n天每天的餐巾用量 a i a_i ai.一块餐巾可以用 P P P元钱购买.
当然你也可以选择把用剩的餐巾进行送洗.快洗一块餐巾需 M M M F F F元,慢洗需 N N N S S S元.
请最小化花费.

这个不好构造最小割,所以应该想到最小费用最大流.(然而就算知道这个也不好想)

一开始想了个这样的图在这里插入图片描述
除了使用边外其他边的容量都为INF.
这个模型的问题在于:由于我跑的是最小费用最大流(是先满足最大流),
那么凡是有送洗,那么其反向边就一定有流量,两边S,T就依然联通.
在这种情况下,送洗会执行反悔操作,即每个餐巾都靠买来.

归根结底,还是晚上的边出现了问题.
晚上有两种选择,但是废弃能保证流量最大且费用最低,所以费用流就会只流这条边,从而脏餐巾永远不会送洗.

怎样才能让它可以送洗呢?
由于每天晚上的餐巾的剩余是固定的,所以我们可以直接从S给它对应容量的边,然后脏餐巾可以通过快洗部/慢洗部送往早上.整个流图大概长这样:
在这里插入图片描述
只有S->晚,早->T的流量有限制,其他都为INF.
那么无论如何,早->T的流量一定会流完(表示满足需求),而晚上的脏餐巾则是选择性地选择送洗,很好地满足了问题.

#include <map>
#include <set>
#include <assert.h>
#include <ctime>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar()  //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pi pair<int, int>
#define pb push_back
#define IT iterator
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2100, M = N * 6, size = 1 << 20, mod = 998244353;
const int inf = 1e9;

// char buf[size],*p1=buf,*p2=buf;
template <class o>
void qr(o &x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c)) x = x * 10 + c - '0', c = gc;
    x *= f;
}
template <class o>
void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
template <class o>
void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    puts("");
}

// math
ll mult(ll a, ll b, ll p) {
    a = (a % p + p) % p;
    b = (b % p + p) % p;
    ll c = (ld)a * b / p;
    return a * b - c * p;
}
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % mod;
        b /= 2;
        a = a * a % mod;
    }
    return c;
}
ll Power(ll a, ll b = mod - 2) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = mult(c, a, mod);
        b /= 2;
        a = mult(a, a, mod);
    }
    return c;
}

int n, p, A, B, C, D, st, ed, d[N], pre[N], f[N], ans;
struct edge {
    int y, d, c, next;
} a[M];
int len = 1, last[N];
void ins(int x, int y, int d, int c) {
    a[++len] = (edge){ y, d, c, last[x] };
    last[x] = len;
}
void add(int x, int y, int d, int c) {
    ins(x, y, d, c);
    ins(y, x, -d, 0);
}

int q[N];
bool v[N];
bool EK() {
    memset(d + 1, 63, ed << 2);
    int l = 0, r = 0;
    q[r++] = st;
    d[st] = 0;
    v[st] = 1;
    f[st] = inf;
    while (l != r) {
        int x = q[l++];
        v[x] = 0;
        if (l == N - 5)
            l = 0;
        for (int k = last[x], y; k; k = a[k].next)
            if (a[k].c && d[y = a[k].y] > d[x] + a[k].d) {
                d[y] = d[x] + a[k].d;
                f[y] = min(f[x], a[k].c);
                pre[y] = k;
                if (!v[y]) {
                    q[r++] = y;
                    if (r == N - 5)
                        r = 0;
                    v[y] = 1;
                }
            }
    }
    if (d[ed] > inf)
        return 0;
    int x = ed, y = f[x];
    ans += y * d[x];
    while (x ^ st) {
        int k = pre[x];
        a[k].c -= y;
        a[k ^ 1].c += y;
        x = a[k ^ 1].y;
    }
    return 1;
}

int main() {
    qr(n);
    qr(p);
    qr(A);
    qr(B);
    qr(C);
    qr(D);
    st = 0;
    ed = n + n + 1;
    for (int i = 1, x; i <= n; i++) {
        qr(x);
        add(st, i + n, p, inf);
        add(st, i, 0, x);
        add(i + n, ed, 0, x);
        if (i + A <= n)
            add(i, i + A + n, B, inf);
        if (i + C <= n)
            add(i, i + C + n, D, inf);
        if (i < n)
            add(i, i + 1, 0, inf);
    }
    while (EK())
        ;
    pr2(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值