# Pleasant sheep and big big wolf

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2254    Accepted Submission(s): 946

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.

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.

Output
For every case:

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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <map>

using namespace std;
const int inf=0x3f3f3f3f;
int n,m,cnt,nv,s,t;
int jx[]={1,0,-1,0};
int jy[]={0,1,0,-1};
struct node
{
int u,v,cap;
int next;
}edge[10000010];

void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;

edge[cnt].v=u;
edge[cnt].cap=0;
}

void bfs()
{
memset(d,-1,sizeof(d));
memset(num,0,sizeof(num));
queue<int >q;
q.push(t);
d[t]=0;
num[0]=1;
while(!q.empty()) {
int i;
int u=q.front();
q.pop();
int v=edge[i].v;
if(d[v]==-1) continue;
d[v]=d[u]+1;
num[d[v]]++;
q.push(v);
}
}
}

void isap()
{
int flow=0, u=pre[s]=s, i;
bfs();
while(d[s]<nv) {
if(u==t) {
int f=inf, pos;
for(i=s; i!=t; i=edge[cur[i]].v) {
if(f>edge[cur[i]].cap) {
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=s; i!=t; i=edge[cur[i]].v) {
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
u=pos;
}
for(i=cur[u]; i!=-1; i=edge[i].next) {
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
break;
}
if(i!=-1) {
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
} else {
if(--num[d[u]]==0) break;
int mind=nv;
if(mind>d[edge[i].v]&&edge[i].cap) {
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
printf("%d\n",flow);
}

int main()
{
int i, j,k;
int tt=1;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
s=0;
t=n*m+1;
nv=t+1;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==1)
if(mp[i][j]==2)
if(i!=1)
if(i!=n)
if(j!=1)
if(j!=m)
}
}
printf("Case %d:\n",tt++);
isap();
}
return 0;
}


©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客