HDU 3046 Pleasant sheep and big big wolf 最小割==最大流(喜羊羊)

点击打开链接

 

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.

 


 

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
 


 

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;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值