bzoj1001:[BeiJing2006]狼抓兔子
传送门
最小割,一眼最小割,这个甚至不需要建什么模型,直接按它给的建就好了,最小割=最大流,跑个最大流就行了,注意:图是无向图
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
#define min(a,b) (a<b?a:b)
#define rg register
int dis[1000010],ans,sum,n,m,cnt=1,s,t,inf=1e9+7,pre[6000001],nxt[6000001],h[1000010],v[6000001],cur[1000010];queue<int>q;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
inline void add(int x,int y,int z)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,v[cnt]=z;
}
inline bool bfs()
{
memset(dis,0,sizeof dis);
q.push(s),dis[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(rg int i=h[x];i;i=nxt[i])if(!dis[pre[i]]&&v[i])dis[pre[i]]=dis[x]+1,q.push(pre[i]);
}
return dis[t];
}
inline int dfs(int x,int flow)
{
if(x==t||!flow)return flow;
int f=flow;
for(rg int &i=cur[x];i;i=nxt[i])
if(v[i]&&dis[pre[i]]>dis[x])
{
int y=dfs(pre[i],min(v[i],f));
f-=y,v[i]-=y,v[i^1]+=y;
if(!f)return flow;
}
if(f==flow)dis[x]=-1;
return flow-f;
}
int main()
{
read(n),read(m),s=1,t=n*m;
for(rg int i=1,x;i<=n;i++)
for(rg int j=1;j<m;j++)read(x),add((i-1)*m+j,(i-1)*m+j+1,x);
for(rg int i=1,x;i<n;i++)
for(rg int j=1;j<=m;j++)read(x),add((i-1)*m+j,i*m+j,x);
for(rg int i=1,x;i<n;i++)
for(rg int j=1;j<m;j++)read(x),add((i-1)*m+j,i*m+j+1,x);
for(;bfs();ans+=dfs(s,inf))memcpy(cur,h,sizeof h);
printf("%d\n",ans);
}