解题报告 P2045 方格取数加强版

· · 个人记录

题目链接:P2045 方格取数加强版

解法:最大费用最大流

如何看出是网络流?

看数据范围, k 不是固定的,总不能开十几维数组跑 dp 吧。而取数之间不独立,不能分开取。

取数特别像费用,只能取一次,流量流过了就加上费用。

但是每个费用只能取一次,所以只好拆点来分流了。

把每个点拆成入点和出点,入点向出点连两条边,一条容量为 1 ,费用为格子内的值,表示每个点上的数只能取一次;另一条容量等于(大于也行) k-1 ,费用为 0 。这样就可以分流了。

所有格子向其下面的点连边,容量等于(大于也行) k ,费用为 0 ,同样向右连边。

最后从源点 S 向 (1,1) 连边,从 (n,n) 向汇点 T 连边,容量均为 k ,费用均为 0 。

跑最大流就可以保证取了 k 次,建模保证了每次取都是合法的。

显然网络是个 DAG , 跑最大费用最大流即可。

时间复杂度 O(n^2m)

AC 代码

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

namespace Qza {

    int n,k,a[52][52],seq[52][52];
    int en[5005],nex[50005],to[50005],w[50005],c[50005];
    int S,T,maxflow,maxcost,cost[5005],flow[5005],pre[5005],las[5005];
    bool vis[5005];
    char buf[10000000], *buf_S,*buf_T;

    queue <int> Q;

    void add_edge(const register int x,const register int y,const register int z,const register int u)
    {
        static int cnt=-1;
        nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=z; c[cnt]=u;
        nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=0; c[cnt]=-u;
    }
    bool spfa(const register int &s,const register int &t) 
    {
        memset(cost,-1,sizeof(cost));
        memset(flow,0x3f,sizeof(flow));
        cost[s]=0;
        while(!Q.empty()) vis[Q.front()]=false, Q.pop();
        Q.push(s);
        vis[s]=true;
        pre[t]=0;
        register int x;
        while(!Q.empty()) {
            x=Q.front(); Q.pop();
            vis[x]=false;
            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];
    }
    inline void Maxflow_Maxcost_EK(const register int &s,const register int &t) 
    {
        while(spfa(s,t)) {
            int cur=t;
            maxflow+=flow[t];
            maxcost+=flow[t]*cost[t];
            while(cur!=s) {
                w[las[cur]]-=flow[t];
                w[las[cur]^1]+=flow[t];
                cur=pre[cur];
            }
        }
    }
    char get_char()
    {
        if(buf_S==buf_T) {
            buf_T=(buf_S=buf)+fread(buf,1,10000000,stdin)-1;
            if(buf_S==buf_T) return EOF;
            return *buf_S;
        }
        return *++buf_S;
    }
    #define getchar get_char
    int read()
    {
        char ch=getchar(); int x=0;
        while(ch<'0' || ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
        return x;
    }
    void main()
    {
        int cnt=0;
        memset(en,-1,sizeof(en));

        n=read(); k=read();
        for(int i=1;i<=n;++i) 
            for(int j=1;j<=n;++j) {
                seq[i][j]=++cnt;
                a[i][j]=read();
            }
        S=cnt<<1|1; T=S+1;
        for(int i=1;i<=n;++i) 
            for(int j=1;j<=n;++j) {
                add_edge(seq[i][j],seq[i][j]+cnt,1,a[i][j]);
                add_edge(seq[i][j],seq[i][j]+cnt,k-1,0);
                if(j!=n) add_edge(seq[i][j]+cnt,seq[i][j+1],k,0);
                if(i!=n) add_edge(seq[i][j]+cnt,seq[i+1][j],k,0);
            }
        add_edge(S,seq[1][1],k,0);
        add_edge(seq[n][n]+cnt,T,k,0);

        Maxflow_Maxcost_EK(S,T);
        printf("%d",maxcost);
    }
    #undef getchar
}

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