P1251 餐巾计划问题 最小费用最大流

  

题目描述

一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同。假设第 ii 天需要 r_iri块餐巾( i=1,2,...,N)。餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送到快洗部,洗一块需 m 天,其费用为 f 分;或者送到慢洗部,洗一块需 nn天(n>mn>m),其费用为 ss 分(s<fs<f)。

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

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

输入输出格式

输入格式:

由标准输入提供输入数据。文件第 1 行有 1 个正整数 NN,代表要安排餐巾使用计划的天数。

接下来的 NN 行是餐厅在相继的 NN 天里,每天需用的餐巾数。

最后一行包含5个正整数p,m,f,n,sp,m,f,n,s。pp 是每块新餐巾的费用; mm 是快洗部洗一块餐巾需用天数; ff 是快洗部洗一块餐巾需要的费用; nn 是慢洗部洗一块餐巾需用天数; ss 是慢洗部洗一块餐巾需要的费用。

输出格式:

将餐厅在相继的 N 天里使用餐巾的最小总花费输出

输入输出样例

输入样例#1: 复制
3
1 7 5 
11 2 2 3 1
输出样例#1: 复制
134

说明

N<=2000

ri<=10000000

p,f,s<=10000

时限4s

一开始根本没想到要拆点

最关键的是  每天都有need【i】条白毛巾变成黑毛巾 这是肯定的  所以源点连黑毛巾 流量为need【i】 费用为0   每天早上要上缴need【i】条毛巾  所以连向汇点  其他的就很简单了

网络流中要开ll 只要把maxflow和mincost 改成ll   非常方便

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=100001;

ll maxflow,mincost;
int last[N],pre[N],dis[N],flow[N];
bool vis[N];
struct Edge{
    int next,to,flow,dis;
}edge[N<<1];
int pos=1,head[N];
void init()
{
    pos=1;
    CLR(head,0);
    mincost=maxflow=0;
}
queue <int> q;
void add(int from,int to,int flow,int dis)//flow流量 dis费用
{
    edge[++pos].next=head[from];
    edge[pos].flow=flow;
    edge[pos].dis=dis;
    edge[pos].to=to;
    head[from]=pos;

    edge[++pos].next=head[to];
    edge[pos].flow=0;
    edge[pos].dis=-dis;
    edge[pos].to=from;
    head[to]=pos;

}
bool spfa(int s,int t)
{
    CLR(dis,0x3f);
    CLR(flow,0x3f);
    CLR(vis,0);
    while (!q.empty()) q.pop();
    dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
    int tot=0;
    while (!q.empty())
    {
        int now=q.front(); q.pop(); vis[now]=0;
        for (int i=head[now]; i; i=edge[i].next)
        {
            int to=edge[i].to;
            if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
            {
                dis[to]=edge[i].dis+dis[now];
                flow[to]=min(edge[i].flow,flow[now]);
                last[to]=i;
                pre[to]=now;
                if (!vis[to])
                {
                    q.push(to); vis[to]=1;
                }
            }
        }
    }
    return pre[t]!=-1;
}
void MCMF(int s,int t)
{
    while (spfa(s,t))
    {
        int now=t;
        maxflow+=flow[t];
        mincost+=flow[t]*dis[t];
        while (now!=s)
        {
            edge[last[now]].flow-=flow[t];//dis . flow
            edge[last[now]^1].flow+=flow[t];
            now=pre[now];
        }
    }
}
int t,n,m,need[N],s,x,y,x2,y2,p;
int main()
{
    RI(n);
    rep(i,1,n)RI(need[i]);
    RIII(p,x,y);RII(x2,y2);
    s=2*n+1,t=s+1;
    rep(i,1,n)add(s,i,need[i],0),add(i+n,t,need[i],0);
    rep(i,1,n)
    {
        if(i+x<=n)add(i,i+n+x,inf,y);
        if(i+x2<=n)add(i,i+n+x2,inf,y2);
        if(i+1<=n)add(i,i+1,inf,0);
        add(s,i+n,inf,p);
    }
    MCMF(s,t);
    cout<<mincost;

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/bxd123/p/10945880.html