【费用流】【网络流24题】【P1251】 餐巾计划问题

Description

一个餐厅在相继的 \(N\) 天里,每天需用的餐巾数不尽相同。假设第 \(i\) 天需要 \(r_i\)块餐巾。餐厅可以购买新的餐巾,每块餐巾的费用为 \(p\) 分;或者把旧餐巾送到快洗部,洗一块需 \(m\) 天,其费用为 \(f\) 分;或者送到慢洗部,洗一块需 \(n\) 天(\(n>m\)),其费用为 \(s\) 分(\(s<f\))。

每天结束时,餐厅必须决定将多少块脏的餐巾送到快洗部,多少块餐巾送到慢洗部,以及多少块保存起来延期送洗。但是每天洗好的餐巾和购买的新餐巾数之和,要满足当天的需求量。

试设计一个算法为餐厅合理地安排好 \(N\) 天中餐巾使用计划,使总的花费最小。编程找出一个最佳餐巾使用计划。

Input

第一行是一个整数 \(N\) 代表天数

下面一行 \(n\) 个数字代表每天的需求

下面一行五个参数分别是 \(p,m,f,n,s\)

Output

一行一个数字代表答案

Hint

\(1~\leq~n~\leq~2000\)

\(1~\leq~r_i~\leq~10^7\)

\(1~\leq~f,p,s~\leq~10000\)

Solution

考虑费用流。

显然需要拆点,把每天拆成上午和晚上。

最初的想法是每天早晨连向晚上来控制当天的需求,然后通过源点连向第一个白天获得新的毛巾,晚上连向白天来洗毛巾,白天到下一天白天,晚上到下一天晚上连边来攒毛巾,最后一天晚上连汇点来。但是事实上由于需要跑满最大流,所以这样的建图方法一定会使得源点流出的流是每天的需求和,从而达到最大流,这样显然不一定是最优的。

考虑这样建图失败的原因是洗毛巾代表的流已经在之前从白天流到黑夜了,为了满足最大流所以这部分一定不能再从白天流向黑夜一次。为了解决这个问题,我们必须让代表洗毛巾的流以前没有从白天到黑夜跑过。于是我们直接从源点连向黑夜,容量为该天的需求,费用为 \(0\),代表该天黑夜获得了当天的脏毛巾。

为了控制满流代表满足需求,我们每天白天连向汇点,容量为当天的需求。剩下的边依然同上,但是去除掉白天之间的边以及白天流向当天晚上的边。

这样我们让代表不同意义的流来源不同,他们对最大流产生的贡献也就相同了,因此可以在这样的基础上最小化费用。

具体的,建图方式为:

容量 费用 意义
源点 每天晚上 当天消耗 0 每天晚上获得当天消耗的脏毛巾
每天早上 汇点 当天消耗 0 当天消耗的毛巾,满流代表满足消耗
每天晚上 \(m\) 天后的早上 INF \(f\) 快洗
每天晚上 \(n\) 天后的早上 INF \(s\) 慢洗
源点 每天早上 INF \(p\) 购买新餐巾
每天晚上 第二天晚上 INF \(0\) 用过的脏餐巾攒下来后面洗

另外注意到数据范围需要开 long long

Code

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif

typedef long long ll;

namespace IPT {
    const int L = 1000000;
    char buf[L], *front=buf, *end=buf;
    char GetChar() {
        if (front == end) {
            end = buf + fread(front = buf, 1, L, stdin);
            if (front == end) return -1;
        }
        return *(front++);
    }
}

template <typename T>
inline void qr(T &x) {
    char ch = IPT::GetChar(), lst = ' ';
    while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
    while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
    if (lst == '-') x = -x;
}

namespace OPT {
    char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
    if (x < 0) {x = -x, putchar('-');}
    int top=0;
    do {OPT::buf[++top] = static_cast<char>(x % 10 + '0');} while (x /= 10);
    while (top) putchar(OPT::buf[top--]);
    if (pt) putchar(aft);
}

const int maxn = 4010;
const int INF = 0x3f3f3f3f;

struct Edge {
    Edge *nxt, *bk;
    int from, to, flow, fee;
};
Edge *hd[maxn], *pre[maxn];
inline void cont(Edge *u, Edge *v, int from, int to, int fl, int fe) {
    u->from = from; u->to = to; u->flow = fl; u->fee = fe; u->bk = v; u->nxt = hd[from]; hd[from] = u;
}
inline void conet(int from, int to, int fl, int fe) {
    Edge *u = new Edge, *v = new Edge;
    cont(u, v, from, to, fl, fe); cont(v, u, to, from, 0, -fe);
}

int n, a, b, c, d, f, s, t;
int MU[maxn], night[maxn], cost[maxn], maxflow[maxn];
bool inq[maxn];
std::queue<int>Q;
ll ans;

bool SPFA();
void argu();

int main() {
    freopen("1.in", "r", stdin);
    qr(n);
    for (int i = 1; i <= n; ++i) qr(MU[i]);
    qr(a); qr(b); qr(c); qr(d); qr(f);
    s = (n << 1) | 1; t = (n << 1) + 2;
    for (int i = 1; i <= n; ++i) night[i] = i + n;
    for (int i = 1; i <= n; ++i) {
        conet(s, night[i], MU[i], 0);
        conet(i, t, MU[i], 0);
        if ((i + b) <= n) conet(night[i], i + b, INF, c);
        if ((i + d) <= n) conet(night[i], i + d, INF, f);
        conet(s, i, MU[i], a);
        if (i != n) conet(night[i], night[i + 1], INF, 0);
    }
    while (SPFA()) argu();
    qw(ans, '\n', true);
    return 0;
}

bool SPFA() {
    memset(inq, 0, sizeof inq);
    memset(cost, 0x3f, sizeof cost);
    memset(maxflow, 0, sizeof maxflow);
    maxflow[s] = INF; cost[s] = 0; Q.push(s);
    while (!Q.empty()) {
        int h = Q.front(); Q.pop(); inq[h] = false; if (!maxflow[h]) continue;
        for (Edge *e = hd[h]; e; e = e->nxt) if (e->flow > 0) {
            int to = e->to;
            if (cost[to] > (cost[h] + e->fee)) {
                cost[to] = cost[h] + e->fee;
                maxflow[to] = std::min(e->flow, maxflow[h]);
                if (!inq[to]) Q.push(to);
                inq[to] = true; pre[to] = e;
            }
        }
    }
    return cost[t] != INF;
}

void argu() {
    for (auto e = pre[t]; e; e = pre[e->from]) {
        e->flow -= maxflow[t];
        e->bk->flow += maxflow[t];
    }
    ans += 1ll * maxflow[t] * cost[t];
}

Summary

同样流过一条边的两个流,如果一个已经对最大流产生过贡献另一个没有,那么最大流一定会选择流过没产生贡献的那个流。解决方法是修改另一个流的路径使得他还没产生过贡献。

猜你喜欢

转载自www.cnblogs.com/yifusuyi/p/10415512.html
今日推荐