解题报告 P2805 [NOI2009]植物大战僵尸

· · 个人记录

题目链接:P2805 [NOI2009]植物大战僵尸。

题目大意

给定 n\times m 的矩阵,矩阵每个位置都有一个权值。现在要取其中若干位置,最大化权值之和。要求:取一个位置,它右边的所有位置都必须取。还有一些特殊要求,形如:若取 (x,y) ,则必取 (i,j) 。

数据规模:1\leqslant n\leqslant20\ ,\ 1\leqslant m\leqslant30\ ,\ -10000\leqslant\text{权值} Score\leqslant10000 。

解法:最大权闭合子图模型

点权有正有负,选点有必要条件,这就是最大权闭合子图的模型了。

这里有个问题,也是本题模型与经典的最大权闭合子图模型的不同之处。

本题中,如果上述过程建的图中有环,则环上的点都不能选。而经典模型中可以都选。

所以本题模型需要一些特殊处理,比方说把所有环上的点设置为不可到达,再跑最大流。

具体的,按照“充分条件”的关系(即 u\to v 表示选取 u 后才能选 v ),计算入度,进行拓扑排序。然后把所有入度不为零的点(包括环上点和环上的点指向的点)高度均设置为足够高,保证求最大流时不会经过这些点。

这里还需要注意,“正权点权值之和”也要相应的改变,要除去正权点中那些被我们预先剔除掉的点,求剩下的正权点的权值之和。

现在我们可以开开心心地跑最大流啦。

注意事项

AC 代码

评测记录。

#include<cstdio>
#include<cstring>
#include<queue>
namespace Qza{
    #define MAXN 605
    #define MAXM 800000
    #define INF 2147483647
    int h[MAXN],gap[MAXN],pre[MAXN],cur[MAXN];
    int en[MAXN],to[MAXM],nex[MAXM],w[MAXM];
    int in[MAXN],id[22][32];
    void add_edge(int x,int y,int v)
    {
        static int cnt=-1;
        nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=v;
        nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=0;
    }
    void bfs(int s,int t)
    {
        register int u,v;
        std::queue<int> q;
        for(h[t]=0,q.push(t);!q.empty();) {
            u=q.front(); q.pop();
            for(int p=en[u];p!=-1;p=nex[p])
                if(w[p^1] && !h[v=to[p]] && v!=t)
                    h[v]=h[u]+1,q.push(v);
        }
    }
    int add_flow(int s,int t)
    {
        int flow=INF,u,p;
        u=t; while(u!=s) p=pre[u],flow=std::min(flow,w[p]),u=to[p^1];
        u=t; while(u!=s) p=pre[u],w[p]-=flow,w[p^1]+=flow ,u=to[p^1];
        return flow;
    }
    int ISAP(int s,int t,int n)
    {
        if(s==t) return INF;
        register int u=s,v,flow=0,minhv;
        register bool found;
        bfs(s,t);
        if(!h[s]) return 0;
        for(int i=1;i<=n;++i) {
            cur[i]=en[i];
            if(h[i]) ++gap[h[i]];
            else if(i!=t) h[i]=n;
        }
        while(h[s]<n) {
            if(u==t) flow+=add_flow(s,t),u=s;
            found=false;
            for(int &p=cur[u];p!=-1;p=nex[p]) 
                if(w[p] && h[v=to[p]]<h[u]) {
                    found=true; u=v; pre[v]=p;
                    break;
                }
            if(!found) {
                if(!(--gap[h[u]])) break;
                minhv=n;
                for(int p=en[u];p!=-1;p=nex[p])
                    if(w[p]) minhv=std::min(minhv,h[to[p]]);    // 不要写成 h[v]了。。。 
                h[u]=minhv+1;
                ++gap[h[u]];
                cur[u]=en[u];
                if(u!=s) u=to[pre[u]^1];
            }
        }
        return flow;
    }
    void find_circle(int n)
    {
        register int u,v;
        std::queue<int> q;
        for(int i=1;i<=n;++i) if(!in[i]) q.push(i);
        while(!q.empty()) {
            u=q.front(); q.pop();
            for(int p=en[u];p!=-1;p=nex[p])
                if(w[p^1]) {
                    --in[v=to[p]];
                    if(!in[v]) q.push(v);
                } 
        }
    }
    int read()
    {
        char ch=getchar(); int x=0,f=1;
        while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
        return x*f;
    }
    void main()
    {
        memset(en,0xff,sizeof(en));
        int n,m,nm,s,t,x,y,score,val_posi=0,maxflow;
        n=read(); m=read(); 
        nm=n*m; s=nm+1; t=s+1;
        for(int i=0;i<n;++i) {
            id[i][0]=i*m+1;
            for(int j=1;j<m;++j) {
                id[i][j]=i*m+j+1;
                add_edge(id[i][j-1],id[i][j],INF);
                ++in[id[i][j-1]];
            }
        }
        for(int i=0,w;i<n;++i)
            for(int j=0;j<m;++j) {
                score=read(); w=read();
                if(score>0) add_edge(s,id[i][j],score);
                else if(score<0) add_edge(id[i][j],t,-score);
                while(w--) {
                    x=read(); y=read();
                    add_edge(id[x][y],id[i][j],INF);
                    ++in[id[x][y]];
                }
            }
        find_circle(nm);
        for(int i=1;i<=nm;++i) 
            if(in[i]) h[i]=t;   // 保证流量流不到。 
        for(int p=en[s];p!=-1;p=nex[p])
            if(w[p] && !h[to[p]]) val_posi+=w[p];   // 统计正权和,钦定删去点不可包括在内。 
        maxflow=ISAP(s,t,t);
        printf("%d",val_posi-maxflow);
        return;
    }
}
int main()
{
    Qza::main();
    return 0;
}