Topic Link: 1565:[noi2009] plants vs. Zombies
Obviously the maximum right closed graph model, but the naked is wrong
Each plant is connected to the position he is attacking, and if a ring is formed, then this change must be no solution.
Further, none of the points protected by this ring can be taken to
So we're going to sort out a collection of plants that we can kill and then do a network flow.
#include <queue> #include <cstdio> #include <cstdlib> #include <iostream> #include <
Algorithm> using namespace std;
const int maxn=1000010;
const int INF=0X7FFFFFFF/3;
int n,m,tot=1,h[maxn],in[maxn],s,t; struct Edge{int to,next,w;}
G[MAXN];
int cur[maxn],vis[maxn],val[maxn],ans=0;
BOOL FLAG[MAXN];
void Add (int x,int y,int z) {in[x]++; G[++tot].to=y; G[tot].next=h[x];h[x]=tot;
G[tot].w=z; G[++tot].to=x; G[tot].next=h[y];h[y]=tot;
g[tot].w=0;
} void Toposort () {queue<int>q; for (int i=s;i<=t;++i) if (!
In[i]) Q.push (i);
while (!q.empty ()) {int U=q.front (); Q.pop (); flag[u]=1;
if (val[u]>0) ans+=val[u];
for (int i=h[u];i;i=g[i].next) {int v=g[i].to; if (!
G[I].W) {in[v]--; if (!
IN[V]) Q.push (v);
BOOL BFs () {for (int i=s;i<=t;++i) vis[i]=-1; queue<int>q; Q.push (S);
vis[s]=0;
while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=h[u];i;i=g[i].next) {int v=g[i].tO
if (Vis[v]==-1&&g[i].w>0&&flag[v]) {vis[v]=vis[u]+1;
Q.push (v);
}}}return Vis[t]!=-1; int dfs (int x,int f) {if x==t| |!
f) return F;
int used=0,w;
for (int i=cur[x];i;i=g[i].next) if (vis[g[i].to]==vis[x]+1) {w=f-used;
W=dfs (G[i].to,min (g[i].w,w)); G[i].w-=w;
G[i^1].w+=w; Used+=w;
if (G[I].W) cur[x]=i;
if (used==f) return used;
} if (!used) vis[x]=-1;
return used;
int dinic () {int ret=0;
while (BFS ()) {for (int i=s;i<=t;++i) cur[i]=h[i];
Ret+=dfs (S,inf);
}return ret;
int main () {scanf ("%d%d", &n,&m); s=0;
t=n*m+1;
for (int i=1;i<=n*m;++i) {scanf ("%d", &val[i]);
if (val[i]>0) Add (S,i,val[i]);
else Add (i,t,-val[i]);
int a,b,c;
scanf ("%d", &a);
for (int j=1;j<=a;++j) {scanf ("%d%d", &b,&c);
Add (B*m+c+1,i,inf);
} if (i%m!=0) Add (I,i+1,inf),//Select the left side must first select the right} toposort ();
printf ("%d", ans-dinic ()); }