解题报告 P1251 餐巾计划问题

· · 个人记录

题目链接:P1251 餐巾计划问题。

题目大意

餐厅需要满足连续 N 天的干净餐巾数,第 i 天需要 r_i 块干净餐巾。

每天的干净餐巾来源有三种:新买一块餐巾,费用为 p;前 m 天送去洗的餐巾,现在洗干净了,可以再利用,费用为 f;前 n 天送去洗的餐巾,现在洗干净了,可以再利用,费用为 s 。

每天用过的餐巾可以拿去洗,也可以保存到第二天再决定如何处理。

问在满足每天的餐巾需求的情况下,最小费用是多少。

数据规模:1\leqslant N\leqslant2000,r_i\leqslant10^7,1\leqslant p,f,s\leqslant10^4。

解法:线性规划网络优化

餐巾数量和费用都是限制,仅靠最大流无法解决,所以使用费用流。

很容易想到,我们的网络里流的流量就代表餐巾块数。如果你想不到,你可以这样想:首先,餐巾数量太大了,必然不是网络流复杂度的组成部分。那么就是边容量或者费用咯,而餐巾数量不定,不同于费用,更像是流量。另外,不管是送洗还是购买,计价方式都是按餐巾的块数,这简直和“总费用 = 单位费用 \times 流量”一模一样。所以我们使用流量来代表餐巾数量。

好吧费用流的复杂度里确实有个最大流流量。。。所以餐巾数还是在复杂度中的。。。

那么现在,开始慢慢将原问题的限制一一对应到我们的网络模型中。

首先,餐巾需求量必然要靠“最大流”的“最大”来满足。由于每一天的餐巾需求量并不相同,且没有单调性,所以我们不能指望用一定容量的边将每一天都串连起来。或者说,我们不能用网络中的流量代表现行流通的餐巾。

那么要使用最大流,必然是每一天向汇点或者源点向每一天连边,容量为当天的需求量。那么这些边在最大流中满流,就会意味着当天的需求被满足。

由于把这些边建给源点比较难以实现餐巾送洗的模型转化,所以把这些边建给汇点。

于是有:每一天向汇点建边,容量为当天餐巾需求量,费用为 0。

现在需要解决“我们不能用网络中的流量代表现行流通的餐巾”这个问题。由于每天都有餐巾被送到汇点那里,并且一去不复返,所以被送去洗的餐巾需要有别的来路。

那么,由于送洗的餐巾和用来保证需求被满足的餐巾不可混为一谈,我们不得不将每一天拆点,一个点用来满足餐巾需求能以最大流的形式被保证,另一个点用来调配餐巾的换洗情况。

姑且称前者为“早上”,后者为“晚上”。形象生动对吧。

拆了点后,便觉神清气爽。

那么我们上面建的边就是每天的早上向汇点建边,容量为当天餐巾需求量,费用为 0。

注意“我们不能用网络中的流量代表现行流通的餐巾”,所以不需要将每天的早上和晚上连起来。早上只是为了满足餐巾需求,晚上只是为了调配餐巾的换洗情况。

考虑每天可能被送洗的餐巾来源,显然只有两个来源:一,今天用过的干净餐巾;二,前一天没拿去送洗的餐巾。

前面说过:“被送去洗的餐巾需要有别的来路”,来路何在?这有源点了。从源点免费拿到数量等同于当天餐巾需求量的流量,就代表当天用脏了的餐巾。

于是源点向每一天晚上建边,容量为当天餐巾需求量,费用为 0。

然后每一天的晚上向下一天的晚上连边,容量为正无穷,费用为 0,表示每天的餐巾可以留到第二天再送洗。

送洗对应了两条边:第 k 天的晚上向第 k+m 天的早上建边,容量为正无穷,费用为 f ;第 k 天晚上向第 k+n 天早上建边,容量为正无穷,费用为 s 。对于每一天,这两条边就对应了两种送洗方式。

至于购买新的餐巾,就让源点向每天早上建边,容量为正无穷(或者当天需求量),费用为 p。

最终建图如下:

显然,每天都买新餐巾是一个合法的最大流,所以最大流一定存在,不用判断每天的需求是否能被满足。

注意事项

AC 代码

评测记录。

#include<cstdio>
#include<cstring>
#include<utility>
#include<deque>
#include<queue>
namespace Qza{
    #define MAXN 4005
    #define MAXM 24050
    #define INF 0x3f3f3f3f3f3f3f3fll
    bool vis[MAXN];
    long long maxflow,mincost,cost,dis[MAXN];
    int cnt=-1,en[MAXN];
    struct Edge {
        int from,to,next;
        long long w,c;
        Edge(int x=0,int y=0,int z=0,long long s=0,long long t=0):
            from(x),to(y),next(z),w(s),c(t) {}
    }e[MAXM];
    typedef std::pair<long long,int> Pair;
    int spfa(int s,int t)
    {
        register int u,v;
        std::deque<int> q;
        memset(dis,0x3f,sizeof(dis));
        for(dis[t]=0,q.push_back(t),vis[t]=true;!q.empty();) {
            u=q.front(); q.pop_front();
            vis[u]=false;
            for(int p=en[u];p!=-1;p=e[p].next)
                if(e[p^1].w && dis[v=e[p].to]>dis[u]+e[p^1].c) {
                    dis[v]=dis[u]+e[p^1].c;
                    if(!vis[v]) {
                        vis[v]=true;
                        if(!q.empty() && dis[v]<dis[q.front()]) q.push_front(v);
                        else q.push_back(v); 
                    }
                }
        } 
        return dis[s]!=INF;
    }
    int Dijkstra(int s,int t)
    {
        register int u,v;
        register Pair now;
        static std::priority_queue<Pair,std::vector<Pair>,std::greater<Pair> > q;
        memset(dis,0x3f,sizeof(dis));
        for(dis[t]=0,q.push(std::make_pair(0,t));!q.empty();) {
            now=q.top(); q.pop();
            if(now.first!=dis[now.second]) continue;
            u=now.second;
            for(int p=en[u];p!=-1;p=e[p].next)
                if(e[p^1].w && dis[v=e[p].to]>dis[u]+e[p^1].c) {
                    dis[v]=dis[u]+e[p^1].c;
                    q.push(std::make_pair(dis[v],v));
                } 
        }
        return dis[s]!=INF;
    }
    void Reduce(int s)
    {
        for(register int i=0;i<=cnt;++i) 
            e[i].c+=dis[e[i].to]-dis[e[i].from];
        cost+=dis[s];
    }
    long long dfs(int u,int t,long long limit,int deep)
    {
        if(u==t) return limit;
        long long res=limit,flow;
        int v;
        vis[u]=true;
        for(int p=en[u];p!=-1;p=e[p].next)
            if(e[p].w && !vis[v=e[p].to] && !e[p].c) {
                //if(cost==4163) printf("%d %d %lld %lld %d\n",u,v,e[p].w,res,deep);
                flow=dfs(v,t,std::min(res,e[p].w),deep+1);
                e[p].w-=flow; e[p^1].w+=flow;
                res-=flow;
                if(!res) return limit;
            }
        return limit-res;
    }
    void Augment(int s,int t)
    {
        long long flow; 
        while(memset(vis,0,sizeof(vis)),(flow=dfs(s,t,INF,1))) {
            maxflow+=flow;
            mincost+=flow*cost;
        }
    }
    void Primal_Dual(int s,int t)
    {
        if(!spfa(s,t)) return;
        Reduce(s); Augment(s,t);
        while(Dijkstra(s,t)) {
            Reduce(s); Augment(s,t);
        }
    }
    void add_edge(int x,int y,long long w,int c)
    {
        e[++cnt]=Edge(x,y,en[x],w,c); en[x]=cnt;
        e[++cnt]=Edge(y,x,en[y],0,-c); en[y]=cnt;
    }
    int read()
    {
        char ch=getchar();int x=0;
        while(ch<'0' || ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar(); 
        return x;
    }
    #define mor(x) (x)
    #define nht(x) ((x)+N)
    void main()
    {
        int N,n,m,s,t,f,p,r;
        memset(en,0xff,sizeof(en));
        N=read();
        s=(N<<1)+1; t=s+1;
        for(register int i=1;i<=N;++i) {
            r=read();
            add_edge(mor(i),t,r,0);
            add_edge(s,nht(i),r,0);
        }
        p=read(); m=read(); f=read(); n=read(); r=read();
        for(register int i=1;i<N;++i) {
            add_edge(s,mor(i),INF,p);
            add_edge(nht(i),nht(i+1),INF,0);
        }
        add_edge(s,mor(N),INF,p);
        for(register int i=1;i<=N-m;++i) add_edge(nht(i),mor(i+m),INF,f);
        for(register int i=1;i<=N-n;++i) add_edge(nht(i),mor(i+n),INF,r);
        Primal_Dual(s,t);
        printf("%lld",mincost);
    }
}
int main()
{
    Qza::main();
    return 0;
} 

这是一份很早以前的 EK 。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;

namespace Qza {

    const int inf=2147483647;
    const int N=2005;

    int n,x,y,z,p,s,f;
    int S,T,en[4005],nex[240005],to[240005],w[240005],c[240005],pre[4005],las[4005];
    long long maxflow,mincost,flow[4005],cost[4005];
    bool vis[4005]; 
    char buf[10000001],*buf_S,*buf_T;
    queue <int> Q;

    void add_edge(const register int x,const register int y,const register int &W,const register int &C)
    {
        static int cnt=-1;
        nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=W; c[cnt]=C;
        nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=0 ; c[cnt]=-C;
    }
    bool spfa(const register int &s,const register int &t)
    {
        memset(flow,0x3f,sizeof(flow));
        memset(cost,0x3f,sizeof(cost));
        //memset(vis,false,sizeof(vis));

        while(!Q.empty()) vis[Q.front()]=false, Q.pop();
        Q.push(s);
        vis[s]=true;
        cost[s]=0;
        pre[t]=0;
        register int x;

        while(!Q.empty()) {
            x=Q.front();
            vis[x]=false;
            Q.pop();
            for(int p=en[x];~p;p=nex[p]) {
                if(w[p] && cost[to[p]]>cost[x]+c[p]) {
                    cost[to[p]]=cost[x]+c[p];
                    pre[to[p]]=x;
                    las[to[p]]=p;
                    flow[to[p]]=flow[x]<w[p]? flow[x]:w[p];
                    if(!vis[to[p]]) {
                        Q.push(to[p]);
                        vis[to[p]]=true;
                    }
                }
            }
        }
        return pre[t];
    }
    void Mincost_Maxflow(const register int &s,const register int &t)
    {
        while(spfa(s,t)) {
            int cur=t;
            maxflow+=flow[t];
            mincost+=flow[t]*cost[t];
            while(cur!=s) {
                w[las[cur]]-=flow[t];
                w[las[cur]^1]+=flow[t];
                cur=pre[cur];
            }
        }
    }
    int read()
    {
        char ch=*++buf_S; int x=0;
        while(ch<'0' || ch>'9') ch=*++buf_S;
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48), ch=*++buf_S;
        return x;
    }

    void main()
    {
        fread(buf,1,10000000,stdin);
        buf_S=buf-1;
        memset(en,-1,sizeof(en));

        n=read();
        S=n<<1|1; T=S+1;
        for(int i=1;i<=n;++i) {
            x=read();
            add_edge(S,i,x,z);
            add_edge(i+n,T,x,z);
        }
        p=read(); x=read(); f=read(); y=read(); s=read();
        for(int i=1;i<=n;++i) {
            add_edge(S,i+n,inf,p); // 买餐巾。
            if(i+y<=n) add_edge(i,i+y+n,inf,s); // 慢洗店。
            if(i+x<=n) add_edge(i,i+x+n,inf,f); // 快洗店。
            if(i+1<=n) add_edge(i,i+1,inf,z); // 放一天。
        }
        Mincost_Maxflow(S,T);
        printf("%lld",mincost);
    }
}


int main()
{
    Qza::main();
    return 0;
}