【洛谷4001】 [ICPC-Beijing 2006]狼抓兔子(最小割)
传送门
Solution
直接跑最小割板子就好了。
代码实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=1000010,NN=1001,Inf=1e9+10;
int id[NN][NN],front[N],cnt,cur[N];
int dep[N],s,t,n,m,tot,sum;
struct node
{
int to,nxt,w;
}e[12000000];
void Add(int u,int v,int w)
{
e[cnt]=(node){v,front[u],w};front[u]=cnt++;
e[cnt]=(node){u,front[v],w};front[v]=cnt++;
}
queue<int>Q;
bool bfs()
{
memset(dep,0,sizeof(dep));
dep[s]=1;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=front[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(!dep[v] && e[i].w)
{
dep[v]=dep[u]+1;Q.push(v);
}
}
}
return dep[t];
}
int dfs(int u,int flow)
{
if(u==t || !flow)return flow;
for(int &i=cur[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(dep[v]==dep[u]+1 && e[i].w)
{
int di=dfs(v,min(e[i].w,flow));
if(di)
{
e[i].w-=di;e[i^1].w+=di;
return di;
}
else dep[v]=0;
}
}
return 0;
}
int Dinic()
{
int flow=0;
while(bfs())
{
for(int i=s;i<=t;i++)cur[i]=front[i];
while(int d=dfs(s,Inf))flow+=d;
}
return flow;
}
int main()
{
n=gi();m=gi();
memset(front,-1,sizeof(front));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)id[i][j]=++tot;
t=tot++;
Add(s,id[1][1],Inf);
Add(id[n][m],t,Inf);
for(int i=1;i<=n;i++)
for(int j=1;j<m;j++)
{
int x=gi();
Add(id[i][j],id[i][j+1],x);
Add(id[i][j+1],id[i][j],x);
}
for(int i=1;i<n;i++)
for(int j=1;j<=m;j++)
{
int x=gi();
Add(id[i][j],id[i+1][j],x);
Add(id[i+1][j],id[i][j],x);
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
{
int x=gi();
Add(id[i][j],id[i+1][j+1],x);
Add(id[i+1][j+1],id[i][j],x);
}
printf("%d\n",Dinic()/2);
return 0;
}