# [NOI2009]植物大战僵尸

amber的最小割论文有详细的讲解法和证明

GZH神犇写了个非递归的ISAP，比我的Dinic快……

//Lib
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
using namespace std;
//Macro
#define	rep(i,a,b)	for(int i=a,tt=b;i<=tt;++i)
#define	drep(i,a,b)	for(int i=a,tt=b;i>=tt;--i)
#define	erep(i,e,x)	for(int i=x;i;i=e[i].next)
#define	irep(i,x)	for(__typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define	sqr(x)	((x)*(x))
#define	pb	push_back
#define	PS	system("pause");
typedef	long long	ll;
typedef	pair<int,int>	pii;
const int oo=~0U>>1;
const double inf=1e100;
const double eps=1e-6;
string name="pvz", in=".in", out=".out";
//Var
struct E
{
int next,node,cap;
}e[1000008],e2[1000008];
queue<int> q;
int n,m,st,ed,tot=1;
int cnt[1008],dis[1008],h[1008],h2[1008],node[28][38],map[28][38],ind[1008],ans,size;
bool vis[1008];
{
e[++tot].next=h[a];e[tot].node=b;e[tot].cap=c;h[a]=tot;
e[++tot].next=h[b];e[tot].node=a;e[tot].cap=0;h[b]=tot;
}
void Toposort()
{
stack<int> s;
int u,v;
rep(i,1,n)
rep(j,1,m)
if(!ind[node[i][j]])s.push(node[i][j]);
while(!s.empty())
{
u=s.top();s.pop();
vis[u]=true;
erep(i,e2,h2[u])
{
v=e2[i].node;
if((--ind[v])==0)s.push(v);
}
}
}
void Build()
{
tot=1;int v;
rep(i,1,n)
rep(j,1,m)
if(vis[node[i][j]])
{
erep(k,e2,h2[node[i][j]])
}
}
void Init()
{
scanf("%d%d",&n,&m);
st=n*m+1;ed=n*m+2;int c,t,a,b;
rep(i,1,n)rep(j,1,m)node[i][j]=++size;
rep(i,1,n)
{
rep(j,1,m)
{
scanf("%d%d",&c,&t);
map[i][j]=c;
rep(k,1,t)
{
scanf("%d%d",&a,&b);a++;b++;
}
}
}
Toposort();
Build();
}
bool BFS()
{
memset(dis,-1,sizeof dis);
dis[ed]=0;q.push(ed);
int u,v;bool flag=false;
while(!q.empty())
{
u=q.front();q.pop();
erep(i,e,h[u])
{
if(dis[v=e[i].node]==-1&&e[i^1].cap)
{
dis[v]=dis[u]+1;
q.push(v);
if(v==st)flag=true;
}
}
}
return flag;
}
int DFS(int u,int low)
{
if(u==ed)return low;
int ret=low,tmp,v;
erep(i,e,h[u])
{
if(!e[i].cap||dis[v=e[i].node]!=dis[u]-1)continue;
tmp=DFS(v,min(low,e[i].cap));
e[i].cap-=tmp;
e[i^1].cap+=tmp;
low-=tmp;
if(low==0)break;
}
if(ret==low)dis[u]=-1;
return ret-low;
}
void Work()
{
int flow;
while(BFS())
while(flow=DFS(st,oo))
ans-=flow;
cout<<ans<<endl;
}
int main()
{
//	freopen((name+in).c_str(),"r",stdin);
//	freopen((name+out).c_str(),"w",stdout);
Init();
Work();
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120