[网络流24题] 餐巾

[网络流24题] 餐巾
时间限制:5 s 内存限制:128 MB
【问题描述】

一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,…,N)。餐厅可以从三种途径获得餐巾。
(1)购买新的餐巾,每块需p分;
(2)把用过的餐巾送到快洗部,洗一块需m天,费用需f分(f< p)。如m=l时,第一天送到快洗部的餐巾第二天就可以使用了,送慢洗的情况也如此。
(3)把餐巾送到慢洗部,洗一块需n天(n>m),费用需s分(s< f)。
在每天结束时,餐厅必须决定多少块用过的餐巾送到快洗部,多少块送慢洗部。在每天开始时,餐厅必须决定是否购买新餐巾及多少,使洗好的和新购的餐巾之和满足当天的需求量Ri,并使N天总的费用最小。

【输入】
输入文件共 3 行,第 1 行为总天数;第 2 行为每天所需的餐巾块数;第 3 行为每块餐巾的新购费用 p ,快洗所需天数 m ,快洗所需费用 f ,慢洗所需天数 n ,慢洗所需费用 s 。
【输出】
一行,最小的费用
【样例】

3
3 2 4
10 1 6 2 3

64
【数据规模】
n<=200,Ri<=50

题解:

  把每天分为二分图两个集合中的顶点Xi,Yi,建立附加源S汇T。
  1、从S向每个Xi连一条容量为ri,费用为0的有向边。
  2、从每个Yi向T连一条容量为ri,费用为0的有向边。
  3、从S向每个Yi连一条容量为无穷大,费用为p的有向边。
  4、从每个Xi向Xi+1(i+1<=N)连一条容量为无穷大,费用为0的有向边。
  5、从每个Xi向Yi+m(i+m<=N)连一条容量为无穷大,费用为f的有向边。
  6、从每个Xi向Yi+n(i+n<=N)连一条容量为无穷大,费用为s的有向边。

  经过分析可以把每天要用的和用完的分离开处理,建模后就是二分图。二分图X集合中顶点Xi表示第i天用完的餐巾,其数量为ri,所以从S向Xi连接容量为ri的边作为限制。Y集合中每个点Yi则是第i天需要的餐巾,数量为ri,与T连接的边容量作为限制。每天用完的餐巾可以选择留到下一天(Xi->Xi+1),不需要花费,送到快洗部(Xi->Yi+m),费用为f,送到慢洗部(Xi->Yi+n),费用为s。每天需要的餐巾除了刚刚洗好的餐巾,还可能是新购买的(S->Yi),费用为p。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf = 1e9;
const int maxn = 1000, maxm = 2000000;
int N, R[maxn], P, K, A, M, B;
int S, T, maxflow, mincost;
struct node {
    int to, next, rest, cost;
} e[maxm];
int head[maxn], cnt = 1;
inline void addedge(int x, int y, int z, int c) {
    e[++cnt].to = y;
    e[cnt].rest = z;
    e[cnt].next = head[x];
    head[x] = cnt;
    e[cnt].cost = c;
    e[++cnt].to = x;
    e[cnt].rest = 0;
    e[cnt].next = head[y];
    head[y] = cnt;
    e[cnt].cost = -c;
}
int dis[maxn], pre[maxn];
bool vis[maxn];
bool SPFA() {
    static queue<int> Q;
    for (int i = S; i <= T; i++)
        dis[i] = inf, vis[i] = false;
    Q.push(S);
    dis[S] = 0;
    vis[S] = true;
    while (!Q.empty()) {
        int x = Q.front();
        Q.pop();
        vis[x] = false;
        for (int i = head[x]; i; i = e[i].next) {
            int y = e[i].to;
            if (e[i].rest && dis[y] > dis[x] + e[i].cost) {
                dis[y] = dis[x] + e[i].cost;
                pre[y] = i;
                if (vis[y] == false) {
                    vis[y] = true;
                    Q.push(y);
                }
            }
        }
    }
    return dis[T] < inf;
}
void update() {
    int flow = inf;
    for (int i = pre[T]; i; i = pre[e[i ^ 1].to])
        flow = min(flow, e[i].rest);
    for (int i = pre[T]; i; i = pre[e[i ^ 1].to]) {
        e[i].rest -= flow;
        e[i ^ 1].rest += flow;
    }
    maxflow += flow;
    mincost += flow * dis[T];
}
void MCF() {
    maxflow = mincost = 0;
    while (SPFA()) {
        update();
    }
}
int main() {
    freopen("napkin.in", "r", stdin);
    freopen("napkin.out", "w", stdout);
    scanf("%d", &N);
    S = 0;
    T = 2 * N + 1;

    for (int i = 1, r; i <= N; i++) {
        scanf("%d", &r);
        addedge(S, i, r, 0);
        addedge(N + i, T, r, 0);
    }
    scanf("%d%d%d%d%d", &P, &K, &A, &M, &B);
    for (int i = 1; i <= N; i++) {
        addedge(S, N + i, inf, P);
        if (i < N)
            addedge(i, i + 1, inf, 0);
        if (i + K <= N)
            addedge(i, i + K + N, inf, A);
        if (i + M <= N)
            addedge(i, i + M + N, inf, B);
    }
    MCF();
    printf("%d", mincost);
    return 0;
}

贪心:
以totalneed(每天的需求餐巾之和)和maxneed(单日需求餐巾最大值)为上下界从上界到下届枚举买的餐巾数;
这些餐巾在第一天直接购买。
每天在过去后有一个遗留的dirtynum[day],先放着不管。
直到所有的新餐巾全部变成了dirtynum[day]中的餐巾,
以后的餐巾就从前面的dirtynum[day]中来,其中优先使用靠前的durtynum[day]来慢洗,
如果不够就用尽量靠后的dirtynum[day]快洗。
如果还不够就宣布程序结束,不必再枚举更少的餐巾购买数了。
在这期间不断更新最优值就可以了。
题库上不用输出具体情况,如果非要输出具体情况,再具体地处理记录一下就可以了

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main(void) {
    freopen("napkin.in", "r", stdin);
    freopen("napkin.out", "w", stdout);
    int n, newcost, fasttime, fastcost, slowtime, slowcost, need[210] = { 0 };
    int newnow, i, j, maxneed = 0, totalneed = 0, mincost, costnow;
    int avanum, neednow, slownum, fastnum, dirtynum[210] = { 0 };
    bool fin = false;
    cin >> n;
    for (i = 1; i <= n; i++) {
        cin >> need[i];
        totalneed += need[i];
        if (maxneed < need[i])
            maxneed = need[i];
    }
    cin >> newcost >> fasttime >> fastcost >> slowtime >> slowcost;
    mincost = newcost * totalneed;
    for (newnow = totalneed - 1; newnow >= maxneed; newnow--) {
        avanum = newnow;
        costnow = newcost * newnow;
        for (i = 1; i <= n; i++) {
            if (avanum >= need[i]) {
                avanum -= need[i];
                dirtynum[i] = need[i];
            } else {
                neednow = need[i] - avanum;
                avanum = 0;
                slownum = 0;
                fastnum = 0;
                /*neednow*/
                for (j = 1; j <= i - slowtime; j++) {
                    if (slownum < neednow) {
                        slownum += dirtynum[j];
                        dirtynum[j] = 0;
                        if (slownum >= neednow) {
                            dirtynum[j] = slownum - neednow;
                            slownum = neednow;
                            break;
                        }
                    }
                }
                neednow -= slownum;
                if (neednow) {
                    for (j = i - fasttime; j >= 1; j--) {
                        if (fastnum < neednow) {
                            fastnum += dirtynum[j];
                            dirtynum[j] = 0;
                            if (fastnum >= neednow) {
                                dirtynum[j] = fastnum - neednow;
                                fastnum = neednow;
                                break;
                            }
                        }
                    }
                }
                neednow -= fastnum;
                if (neednow) {
                    fin = true;
                    break;
                }
                costnow += slownum * slowcost + fastnum * fastcost;
                dirtynum[i] = need[i];
            }
        }
        if (fin) {
            break;
        } else {
            if (mincost > costnow)
                mincost = costnow;
        }
    }
    cout << mincost << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值