首页 > 代码库 > BZOJ 1565 植物大战僵尸(拓扑排序+最大权闭合子图)

BZOJ 1565 植物大战僵尸(拓扑排序+最大权闭合子图)

图中的保护关系就类似于最大权闭合子图。即你想杀x,你就一定要杀掉保护x的点,那么把x向保护它的点连边。那么题目就转化成了最大权闭合子图的问题。

但是这个图有点特殊啊。。。 考虑有环的情况,显然这个环以及指向这个环的点都不能选。

所以还要把这个图的反图进行一遍拓扑排序,这样忽略掉了这些点了。。。

 

技术分享
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-9
# define MOD 1024523
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=605;
//Code begin...

struct Edge{int p, next, w;}edge[N*N];
int head[N], cnt=2, cost[N], dee[N], n, m, s, t, vis[N];
bool mark[N];
queue<int>Q;
vector<PII> E[N];

void add_edge(int u, int v, int w){
    edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
    edge[cnt].p=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++;
}
int bfs(){
    int i, v;
    mem(vis,-1);
    vis[s]=0; Q.push(s);
    while (!Q.empty()) {
        v=Q.front(); Q.pop();
        for (i=head[v]; i; i=edge[i].next) {
            if (edge[i].w>0 && vis[edge[i].p]==-1) {
                vis[edge[i].p]=vis[v] + 1;
                Q.push(edge[i].p);
            }
        }
    }
    return vis[t]!=-1;
}
int dfs(int x, int low){
    int i, a, temp=low;
    if (x==t) return low;
    for (i=head[x]; i; i=edge[i].next) {
        if (edge[i].w>0 && vis[edge[i].p]==vis[x]+1){
            a=dfs(edge[i].p,min(edge[i].w,temp));
            temp-=a; edge[i].w-=a; edge[i^1].w += a;
            if (temp==0) break;
        }
    }
    if (temp==low) vis[x]=-1;
    return low-temp;
}
void Topsort(){
    FO(i,0,n*m) if (!dee[i]) Q.push(i), mark[i]=true;
    while (!Q.empty()) {
        int u=Q.front(); Q.pop();
        FO(i,0,E[u].size()) {
            PII v=E[u][i];
            if (!v.second) continue;
            --dee[v.first];
            if (!dee[v.first]) Q.push(v.first), mark[v.first]=true;
        }
    }
}
int main ()
{
    int T, x, y, res=0, tmp, sum=0;
    scanf("%d%d",&n,&m); s=n*m; t=n*m+1;
    FO(i,0,n) FO(j,0,m) {
        scanf("%d%d",&cost[i*m+j],&T);
        while (T--) {
            scanf("%d%d",&x,&y); E[x*m+y].pb(mp(i*m+j,0));
            E[i*m+j].pb(mp(x*m+y,1)); ++dee[x*m+y];
        }
    }
    FO(i,0,n) FO(j,0,m-1) FO(k,j+1,m) E[i*m+j].pb(mp(i*m+k,0)), E[i*m+k].pb(mp(i*m+j,1)), ++dee[i*m+j];
    Topsort();
    FO(i,0,n*m) {
        if (!mark[i]) continue;
        if (cost[i]>=0) res+=cost[i], add_edge(s,i,cost[i]);
        else add_edge(i,t,-cost[i]);
        FO(j,0,E[i].size()) {
            PII v=E[i][j];
            if (v.second||!mark[v.first]) continue;
            add_edge(i,v.first,INF);
        }
    }
    while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
    printf("%d\n",res-sum);
    return 0;
}
View Code

 

BZOJ 1565 植物大战僵尸(拓扑排序+最大权闭合子图)