hdu 3046 Pleasant sheep and big big wolf (最小割)

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

 

题意 : 有狼和羊在一个 n*m网格上 ; 要求用栏杆把狼和羊隔离开,问最小用栏杆数量 ; 1是羊,2是狼 ;

每个点跟相邻的四个点连边,容量为1 ;意义在于,就相当于把这个点围起来用4个栏杆;每条边相当于一个栏杆;

注意边界判断,有些相邻的没有4个点 ;

然后狼连源点,容量为inf ; 羊连汇点,容量为inf ; 意义在于,这样保证了可以求最小割,把整个网络分为俩个图;

并且肯定是狼在一个源点那个图,羊在汇点那个图,因为他们容量为inf ,所以最小割不会去割他们; 而最小割切断的那些边,就是要围的栏杆数量!!!

 

#include<cstdio>
#include<cstring>
const int N=50000;
const int M=500000;
const int inf=0x7fffffff;
int head[N];
int map[201][201];
int pre[N],cur[N],dis[N],gap[N];
struct Edge
{
    int v,next,w;
} edge[M];
int top,n,s,t;
void addedge(int u,int v,int w)
{
    edge[top].v=v;
    edge[top].w=w;
    edge[top].next=head[u];
    head[u]=top++;
    edge[top].v=u;
    edge[top].w=0;
    edge[top].next=head[v];
    head[v]=top++;
}
int sap()
{
    
    int flow=0,aug=inf,u;
    memset(dis,0,sizeof(dis));
	memset(gap,0,sizeof(gap));
    for(int i=0; i<n; i++)          cur[i]=head[i];         
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        
        loop :
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(v==t)     
				{
					for(u=pre[v];v!=s;v=u,u=pre[u])
					{
						edge[cur[u]].w-=aug;
						edge[cur[u]^1].w+=aug;
					}
					flow+=aug;
					aug=inf;
				}              
               goto loop ;
            }
        }
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
int main()
{
    int m;
    int cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        s=0;
        t=n*m+1;
        top=0;
        memset(head,-1,sizeof(head));
        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 tmp=(i-1)*m+j;
              if(i<n) addedge(tmp,tmp+m,1);    //连上点 
              if(i>1) addedge(tmp,tmp-m,1);    //连下点 
              if(j<m) addedge(tmp,tmp+1,1);    //连右点 
              if(j>1) addedge(tmp,tmp-1,1);    //连左点 
              if(map[i][j]==1) addedge(tmp,t,inf);    //羊连汇点 
              if(map[i][j]==2) addedge(s,tmp,inf);    //羊林源点 
          }
        n=n*m+2;
        printf("Case %d:\n",cas++);
        printf("%d\n",sap());
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值