给一个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的更多相关文章

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

    HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...

  2. 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 ...

  3. 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 ...

  4. 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 a ...

  5. hdu-3046-Pleasant sheep and big big wolf(最大流最小割)

    题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...

  6. HDU 3046Pleasant sheep and big big wolf(切最小网络流)

    职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...

  7. Pleasant sheep and big big wolf

    pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

随机推荐

  1. 什么是 CI/CD?(翻译)

    CI/CD是什么? 原文:https://opensource.com/article/18/8/what-cicd 在谈论软件开发时,经常会提到持续集成Continuous Integration( ...

  2. Flutter - 左右侧滑菜单:drawer和endDrawer

    侧滑菜单可以从左面滑出,也可以从右面滑出.在Scaffold中有drawer和endDrawer两个参数,分别对应左边的菜单和右边的菜单. drawer: new Drawer( child: new ...

  3. .net如何发送格式化的文本内容

    MailMessage mailMessage = new MailMessage();ArrayList attachsendObject = new ArrayList();string mail ...

  4. django 部署一个简单的博客系统

    转:https://www.cnblogs.com/fnng/p/3737964.html 写的目的, 加深影响,熟悉开发流程, 开发都是练出来的. 环境 python3.5 windows 7 1. ...

  5. Linux shell(4)

    test比较两个值: 测试比较两个值是linux中常用的比较运算,test命令可以进行对两个值的比较,如果比较成功则返回值为0,否则为非0 常用比较方法: ·1.整数比较 2.字符串比较 3.逻辑比较 ...

  6. Linux之linux入门

    学习linux之前先了解一下操作系统: 操作系统的定义:         操作系统(英语:operating system,缩写作 OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内 ...

  7. Appium知识积累

    1.使用uiautomatorviewer 可以直接在命令行输入uiautomatorviewer,打开获取屏幕截图工具,连接手机,打开所要获取包名的应用,然后获取其截图,根据截图查看package即 ...

  8. react-native初体验(2) — 认识路由

    如果学习止步于 hello world, 那么人生也太没意思了.这次要做一个看起来真实的应用.多添加几个页面,让他们可以交互,动起来. react-native 官方推荐使用 react-naviga ...

  9. docker 一篇文章学习容器化

      什么是镜像?什么是容器?   一句话回答:镜像是类,容器是实例   docker 基本操作命令:   删除所有container: docker rm $(docker ps -a -q)   删 ...

  10. Oracle和sqlserver数据类型对应

    Sqlserver类型 Oracle类型 binary     RAW(50) bit     NUMBER(2) char     CHAR(10) datetime     DATE decima ...