Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3142 Accepted Submission(s): 1297
Problem Description
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.

Input
There are many cases.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
Output
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
First line output “Case p:”, p is the p-th case;
The second line is the answer.
Sample Input
4 6 1 0 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 0 0 2 0 1 1 0
Sample Output
Case 1: 4
#include<bits/stdc++.h>
using namespace std;
#define inf 0xf3f3f3f
int n,m;
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int level[40004],iter[40004];
struct node
{
int to,cap,rev;
};
vector<node>G[40004];
void add_edge(int from,int to,int cap)
{
G[from].push_back((node){to,cap,G[to].size()});
G[to].push_back((node){from,0,G[from].size()-1});
}
void add()
{
int M=max(n,m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int x;scanf("%d",&x);
if(x==1)add_edge(0,(i-1)*M+j,inf);
else if(x==2)add_edge((i-1)*M+j,M*M+1,inf);
for(int k=0;k<4;k++)
{
int xx=i+d[k][0],yy=j+d[k][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
{
add_edge((i-1)*M+j,(xx-1)*M+yy,1);
}
}
}
}
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
level[s]=0;
queue<int>P;P.push(s);
while(!P.empty())
{
int v=P.front();P.pop();
for(int i=0;i<G[v].size();i++)
{
node e=G[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
P.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
node &e=G[v][i];
if(e.cap>0&&level[e.to]>level[v])
{
int d=dfs(e.to,t,min(e.cap,f));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(1)
{
bfs(s);
if(level[t]<0)return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>0)flow+=f;
}
}
int main()
{
int cas=0;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=40002;i++)G[i].clear();
add();
printf("Case %d:\n",++cas);
printf("%d\n",max_flow(0,max(n,m)*max(n,m)+1));
}
return 0;
}