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): 2757    Accepted Submission(s): 1135


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

思路:最小割=最大流,所以我们只要把狼和羊联立起来建图,跑一遍最大流即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 220
#define INF 99999999
int ma[N][N];
struct Edge
{
    int v,next,cap;
}edge[300000];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int cnt,head[N*N],vis[N*N],d[N*N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap)
{
    edge[cnt].v=v; edge[cnt].cap=cap;
    edge[cnt].next=head[u];head[u]=cnt++;

    edge[cnt].v=u; edge[cnt].cap=0;
    edge[cnt].next=head[v];head[v]=cnt++;
}
int bfs(int s,int t,int n)
{
     memset(vis,0,sizeof(vis));
     memset(d,-1,sizeof(d));
     vis[s]=1;
     d[s]=0;
     queue<int>que;
     que.push(s);
     while(!que.empty())
     {
         int u=que.front();
         que.pop();
         vis[u]=0;
         for(int i=head[u];i!=-1;i=edge[i].next)
         {
             int v=edge[i].v;
             if(d[v]==-1&&edge[i].cap>0)
             {
                 d[v]=d[u]+1;
                 if(!vis[v])
                 {
                     vis[v]=1;
                     que.push(v);
                 }
             }
         }
     }
     return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(d[v]==d[s]+1&&edge[i].cap>0)
        {
            int x=min(f-flow,edge[i].cap);
            x=dfs(v,t,x);
            flow+=x;
            if(x)
            {
                edge[i].cap-=x;
                edge[i^1].cap+=x;
            }
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t,int n)
{
    int flow=0,f;
    while(bfs(s,t,n))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int main()
{
    int n,m,tot=1;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            scanf("%d",&ma[i][j]);
        int s=0,t=n*m+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                for(int k=0;k<4;k++)
                {
                    int x=i+dir[k][0],y=j+dir[k][1];
                    if(x<1||x>n||y<1||y>m) continue;
                    addedge((i-1)*m+j,(x-1)*m+y,1);
                }
                if(ma[i][j]==1)
                    addedge((i-1)*m+j,t,INF);
                else if(ma[i][j]==2)
                    addedge(s,(i-1)*m+j,INF);
            }
        }
        int ans=Dinic(s,t,t);
        printf("Case %d:\n",tot++);
        printf("%d\n",ans);
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值