给一个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. js简单时间比较的方法(转)

    //时间比较(yyyy-MM-dd) function compareDate(startDate, endDate) {   var arrStart = startDate.split(" ...

  2. 【译】Serverless架构 - 3

    原文: https://martinfowler.com/articles/serverless.html 消息驱动型应用 后台数据处理服务是一个不同的例子. 你要写一个需要快速响应UI请求的以用户为 ...

  3. 用Micro:bit控制遥控车

    很多遥控车是用Arduino来控制,同样也可以用Micro:bit来控制.这篇文章我们就来做个测试. 这次需要用到扩展板,管脚比较多,请参考下图 一.材料: •micro:bit 二片 •micro: ...

  4. cookie记住密码/base64加密(js控制)

    cookie记住密码/base64加密(js控制) • 配置cookie //设置cookie function setCookie ( name, value, expdays ) { var ex ...

  5. JUC——线程同步锁(LockSupport阻塞原语)

    java.util.concurrent.locks.LockSupport这个是一个独立的类,这个类的主要功能是用来解决Thread里面提供的suspend()(挂起线程).resume()(恢复运 ...

  6. 管理项目中的贴图-Texture overview插件

    Texture overview插件管理项目中的贴图 1. Assetstore地址 2. 总览项目中所有贴图 3. 针对不同平台的贴图压缩设置 在插件的右上角 4. 支持多选批量修改 5. 点击表头 ...

  7. JetBrains激活 PyCharm | IntelliJ IDEA | CLion | WebStorm...

    最近,JetBrains的IDE火了起来,身为学Java的人,放弃了Eclipse,选择了Idea,还真有点不舍得呢... 虽然Idea不错(在我看来,比Eclipse好用),但是,人家是收费的呀.. ...

  8. Maven ResourceBundle.getBundle读取Properties异常MissingResourceException: Can't find bundlei解决方法

    参考:https://blog.csdn.net/thousa_ho/article/details/72817616 问题描述 ResourceBundle读取properties配置文件提示 Mi ...

  9. PHP中你应该知道的require()文件包含的正确用法

    以前看一些PHP框架源码的时候,很奇怪在文件包含的时候,会用dirname(__FILE__)来拼凑文件路径,不知道这样做有什么好处,后来终于发现了其中的缘由. 我们来看一个简单的例子: 有a,b,c ...

  10. Oracle中的SQL分页查询原理和方法详解

    Oracle中的SQL分页查询原理和方法详解 分析得不错! http://blog.csdn.net/anxpp/article/details/51534006