Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1711 Accepted Submission(s): 739
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.
![](https://i-blog.csdnimg.cn/blog_migrate/13411636bc7c42d46b19b809406ae4db.jpeg)
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
Source
Recommend
gaojie
题意是说有狼和羊在一个图上,让你求最少栏杆使狼和羊分开。既求最小割,也是最大流。此题难在建图上,将狼和源点相连,容量为inf,羊和汇点相连容量为inf,点和周围四个点相连(周围有四个点的,即要判断)相连,容量是1.然后求最大流即可。
#include<stdio.h>
#include<string.h>
#define inf 99999999
#define MIN(a,b) a>b?b:a;
struct E
{
int v,w,next;
E() {}
E(int v,int w,int next):v(v),w(w),next(next) {}
} edg[500007];
int dis[50007],gap[50007],list[50007],nodes;
int n,m;
int map[207][207];
int sourse,sink,nn,node,e;
void addedge(int u,int v,int w)
{
edg[nodes]=E(v,w,list[u]);
list[u]=nodes++;
edg[nodes]=E(u,0,list[v]);
list[v]=nodes++;
}
int dfs(int src,int aug)
{
if(src==sink)return aug;
int left=aug,mindis=nn;
for(int j=list[src]; j!=-1; j=edg[j].next)
{
int v=edg[j].v;
if(edg[j].w)
{
if(dis[v]+1==dis[src])
{
int minn=MIN(left,edg[j].w);
minn=dfs(v,minn);
edg[j].w-=minn;
edg[j^1].w+=minn;
left-=minn;
if(dis[sourse]>=nn)return aug-left;
if(left==0)break;
}
if(dis[v]<mindis)
mindis=dis[v];
}
}
if(left==aug)
{
if(!(--gap[dis[src]]))dis[sourse]=nn;
dis[src]=mindis+1;
gap[dis[src]]++;
}
return aug-left;
}
int sap(int s,int e)
{
int ans=0;
nn=e+1;
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=nn;
sourse=s;
sink=e;
while(dis[sourse]<nn)
ans+=dfs(sourse,inf);
return ans;
}
int main()
{
int cas=1,e;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(list,-1,sizeof(list));
nodes=0;
int p,s=0,sum=0,maxx=0;
sourse=0;
e=n*m+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&map[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int ans=(i-1)*m+j;
if(i<n)addedge(ans,ans+m,1);//和下面的点相连
if(i>1)addedge(ans,ans-m,1);//和上面的点相连
if(j<m)addedge(ans,ans+1,1);//和右面的点相连
if(j>1)addedge(ans,ans-1,1);//和左面的点相连
if(map[i][j]==1)addedge(ans,e,inf);
if(map[i][j]==2)addedge(0,ans,inf);
}
int anss=sap(0,e);
printf("Case %d:\n",cas++);
printf("%d\n",anss);
}
return 0;
}