码迷,mamicode.com
首页 > 其他好文 > 详细

餐巾计划问题(费用流)

时间:2021-02-20 11:47:22      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:namespace   cos   std   size   const   ios   line   sizeof   main   

题意

一个餐厅在相继的 \(N\) 天里,每天需用的餐巾数不尽相同。假设第 \(i\) 天需要 \(r_i\) 块餐巾 (\(i=1,2,\dots,N\))。

餐厅可以购买新的餐巾,每块餐巾的费用为 \(p\) 分;或者把旧餐巾送到快洗部,洗一块需 \(m\) 天,其费用为 \(f\) 分;或者送到慢洗部,洗一块需 \(n\) 天,其费用为 \(s\) 分。

餐厅每天使用的餐巾必须是今天刚购买的,或者是今天刚洗好的,且必须恰好提供 \(r_i\) 块毛巾,不能多也不能少。

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

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

思路

这道题的思路挺神奇的。就是把每一天拆成两个点,一个点指的是旧餐巾,另一个点指的是需要的餐巾数量。

首先每天需要的餐巾数量向汇点\(T\)连容量是\(r_i\),费用是\(0\)的边。

对于每天需要的餐巾数量,有三个来源,一个是直接购买(源点\(S\)向其连容量是\(\infty\),费用是\(p\)的边);可以通过快洗部(相应旧餐巾向其连容量是\(\infty\),费用是\(f\)的边);可以通过慢洗部(相应旧餐巾向其连容量是\(\infty\),费用是\(s\)的边)。

然后再考虑旧餐巾。因为每天都会使用\(r_i\)的餐巾,这部分就成为了旧餐巾,因此源点\(S\)向旧餐巾连容量是\(r_i\),费用是\(0\)的点;旧餐巾可以留到下一天,因此每天的旧餐巾可以向下一天连容量是\(\infty\),费用是\(0\)的边。

跑最小费用流就可。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 1610, M = 10010, inf = 1e8;

int n, p, d1, c1, d2, c2, S, T;
int h[N], e[M], ne[M], f[M], w[M], idx;
int d[N], pre[N], incf[N];
bool st[N];

void add(int a, int b, int c, int d)
{
    e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx ++;
    e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx ++;
}

bool spfa()
{
    memset(d, 0x3f, sizeof(d));
    memset(incf, 0, sizeof(incf));
    queue<int> que;
    que.push(S);
    d[S] = 0, incf[S] = inf;
    st[S] = true;
    while(que.size()) {
        int t = que.front();
        que.pop();
        st[t] = false;
        for(int i = h[t]; ~i; i = ne[i]) {
            int ver = e[i];
            if(f[i] && d[ver] > d[t] + w[i]) {
                d[ver] = d[t] + w[i];
                pre[ver] = i;
                incf[ver] = min(incf[t], f[i]);
                if(!st[ver]) {
                    que.push(ver);
                    st[ver] = true;
                }
            }
        }
    }
    return incf[T] > 0;
}

int EK()
{
    int cost = 0;
    while(spfa()) {
        int t = incf[T];
        cost += t * d[T];
        for(int i = T; i != S; i = e[pre[i] ^ 1]) {
            f[pre[i]] -= t;
            f[pre[i] ^ 1] += t;
        }
    }
    return cost;
}

int main()
{
    scanf("%d%d%d%d%d%d", &n, &p, &d1, &c1, &d2, &c2);
    memset(h, -1, sizeof(h));
    S = 0, T = 2 * n + 1;
    for(int i = 1; i <= n; i ++) {
        int r;
        scanf("%d", &r);
        add(S, i, r, 0);
        add(i + n, T, r, 0);
        add(S, n + i, inf, p);
        if(i + 1 <= n) add(i, i + 1, inf, 0);
        if(i + d1 <= n) add(i, i + d1 + n, inf, c1);
        if(i + d2 <= n) add(i, i + d2 + n, inf, c2);
    }
    printf("%d\n", EK());
    return 0;
}

餐巾计划问题(费用流)

标签:namespace   cos   std   size   const   ios   line   sizeof   main   

原文地址:https://www.cnblogs.com/miraclepbc/p/14413679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!