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的更多相关文章
- HDU 3046 Pleasant sheep and big big wolf(最小割)
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.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 ...
- 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 ...
- 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 ...
- hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...
- HDU 3046Pleasant sheep and big big wolf(切最小网络流)
职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...
- Pleasant sheep and big big wolf
pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
随机推荐
- linux下的python3,virtualenv,Mysql,nginx,redis安装配置
Mysql安装和使用:点我 Redis安装和使用:点我 centos7安装Python3以及tab补全键的使用:点我 Linux下的virtualenv:点我 nginx的安装和使用:点我
- 火狐浏览器油猴子GreaseMonkey使用教程
火狐浏览器油猴子GreaseMonkey使用教程 首先下载火狐浏览器 安装成功后用火狐浏览器打开链接,界面如下 然后搜索GreaseMonkey. 搜索结果,图标是个小猴子,然后添加到火狐浏览器,成功 ...
- JUC——线程同步辅助工具类(Semaphore,CountDownLatch,CyclicBarrier)
锁的机制从整体的运行转态来讲核心就是:阻塞,解除阻塞,但是如果仅仅是这点功能,那么JUC并不能称为一个优秀的线程开发框架,然而是因为在juc里面提供了大量方便的同步工具辅助类. Semaphore信号 ...
- DMA-Direct Memory Access
什么是? DMA 首先它是一种数据的传输方式:其次传输包括从设备到内存.内存到存储.内存到内存等:最后,它是在不经过cpu的情况下实现数据传输.. DAM主要有共享缓存DMA,Scatter/Gath ...
- MATLAB复制图片时边框大的问题
当使用MATLAB画图时,需要将图片复制到word中,会发现图片有一个白色的边框,在论文的排版中是一个影响美观的问题 例如: >> x = 0:10; >> y = sin(x ...
- Redis初探(windows/linux安装)
最近在学习Redis,先看看简介: Redis 是完全开源免费的,遵守BSD协议(可以自由的使用,修改源代码的协议,当然需要满足一定的条件),是一个高性能的key-value数据库. 特点&& ...
- Netty源码分析第5章(ByteBuf)---->第4节: PooledByteBufAllocator简述
Netty源码分析第五章: ByteBuf 第四节: PooledByteBufAllocator简述 上一小节简单介绍了ByteBufAllocator以及其子类UnPooledByteBufAll ...
- XSS跨站脚本
1.反射型 非持久化,需要用户自己点击才可以触发 通常出现在搜索框 <?php $id=$_GET['id']; echo $id; ?> http://127.0.0.1/test/sc ...
- [Notice]博客地址转移 vitostack.com
个人博客地址转移至vitostack.com 这里可能不会经常更新. 欢迎访问新地址.
- React Native (0.57)开发环境搭建(注意:Node不要随便更新到最新版,更新完后莫名其妙的问题一大堆)
搭建开发环境 一.安装依赖 必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 1.首先安装 Homebrew 2.安装 Node, Watchm ...