HDU3046_Pleasant sheep and big big wolf

给一个n*m的数字阵,1表示羊的位置,2表示狼的位置,0表示没有东西,可以通过。在每个格子的4边都可以建立围栏,有围栏的话狼是不能通过的。

现在求最少建立多少围栏能够保证狼无法接触到羊。

题目的模型很简单,直接建立一个超级源点和超级汇点,狼连接远点流量无穷大,羊连接汇点流量无穷大,每个格子和四周的四个格子建立一条流量为1的边,要把狼羊分离就是求最小割了,最大流等于最小割,圆满解决问题。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 100100
#define inf 99999999
using namespace std;

int to[maxn],c[maxn],first[maxn],next[maxn],N;
int d[maxn];
int s,t,n,m,tmp,ans,cas=0;
int Q[maxn],bot,top,tag[maxn],can[maxn],TAG=423;

void _init()
{
    ans=s=0,t=n*m+1,N=-1;
    for (int i=s; i<=t; i++) first[i]=-1;
}

void edge(int U,int V,int W)
{
    N++;
    to[N]=V,c[N]=W;
    next[N]=first[U],first[U]=N;
}

void _input()
{
    int cur=0;
    for (int i=1; i<=n; i++)
        for (int j=1; j<=m; j++)
        {
            scanf("%d",&tmp);
            cur++;
            if (i<n) edge(cur,cur+m,1),edge(cur+m,cur,1);
            if (j<m) edge(cur,cur+1,1),edge(cur+1,cur,1);
            if (tmp==2) edge(s,cur,inf),edge(cur,s,inf);
                else if (tmp==1) edge(cur,t,inf),edge(t,cur,inf);
        }

}

bool bfs()
{
    TAG++;
    Q[bot=top=1]=t,d[t]=0,tag[t]=TAG;
    while (bot<=top)
    {
        int cur=Q[bot++];
        for (int i=first[cur]; i!=-1; i=next[i])
        {
            if (c[i^1]<=0 || tag[to[i]]==TAG) continue;
            tag[to[i]]=TAG,d[to[i]]=d[cur]+1,Q[++top]=to[i];
            if (to[i]==s) return true;
        }
    }
    return false;
}

int dfs(int cur,int num)
{
    if (cur==t) return num;
    int tmp=num,k;
    for (int i=first[cur]; i!=-1; i=next[i])
    {
        if (d[cur]!=d[to[i]]+1 || c[i]<=0 || tag[to[i]]!=TAG || can[to[i]]==TAG) continue;
        k=dfs(to[i],min(num,c[i]));
        if (k) c[i]-=k,c[i^1]+=k,num-=k;
        if (num==0) break;
    }
    if (num) can[cur]=TAG;
    return tmp-num;
}

int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        _init();
        _input();
        while (bfs()) ans+=dfs(s,inf);
        printf("Case %d:\n%d\n",++cas,ans);
    }
    return 0;
}

  

HDU3046_Pleasant sheep and big big wolf

时间: 2024-08-01 00:47:15

HDU3046_Pleasant sheep and big big wolf的相关文章

Pleasant sheep and big big wolf

点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼,每个格子只能存在0或1只动物.现在要用栅栏将所有的狼和羊分开,问怎么放,栅栏数放的最少,求出个数? 解析:将狼群看作一个集合,羊群看作一个集合.然后设置源点和汇点,将两点至存在动物的点的距离赋值为1,构图,由于求得是栅栏数,从存在动物的位置向四周发散点赋值为1,即该方向放置一个栅栏.然后可以发现变成了求最小割,即求出最大流.需要注意的是,由于数据比较大,200 * 200.如果设置源点和汇点相差较大(即s = 0,e = n *

HDU 3046Pleasant sheep and big big wolf(网络流之最小割)

题目地址:HDU 3046 最小割第一发!其实也没什么发不发的...最小割==最大流.. 入门题,但是第一次入手最小割连入门题都完全没思路...sad..对最小割的本质还是了解的不太清楚.. 这题就是对每两个相邻的格子的边界都要进行加边,然后求最大流就OK了. RE了好长时间,注意遍历加边的时候要从1开始,而不是0开始,因为0是源点的...(也许只有我才犯这种错误吧...)建图不多说了..只要了解了最小割,建图还是很容易想的. 代码如下: #include <iostream> #includ

HDU 3046 Pleasant sheep and big big wolf

Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 304664-bit integer IO format: %I64d      Java class name: Main In ZJNU, there is a well-known prairie. And it attracts pleasant

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): 2254    Accepted Submission(s): 946 Problem Description In ZJNU, there is a well-known prairie. And it attracts p

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

HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊,2是狼,问最少要多少围墙才能把狼全部围住,每有到达羊的路径 思路:有羊和狼,要分成两个集合互不可达,显然的最小割,建图源点连狼,容量无穷,羊连汇点,容量无穷,然后相邻格子连边,容量为1 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm

hdoj--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): 2699    Accepted Submission(s): 1114 Problem Description In ZJNU, there is a well-known prairie. And it attrac

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

Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2371    Accepted Submission(s): 988 Problem Description In ZJNU, there is a well-known prairie. And it attracts p

hdoj 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): 2570    Accepted Submission(s): 1056 Problem Description In ZJNU, there is a well-known prairie. And it attracts

hdu 3046 Pleasant sheep and big big wolf 最小割

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 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 A