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题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
随机推荐
- CF刷题-Codeforces Round #481-G. Petya's Exams
题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...
- 如何fork比特币的源码并同步更新到本地
一.首先在Github上fork比特币源码,就会在自己的项目库里创建一份比特币代码的拷贝. 打开https://github.com/bitcoin/bitcoin,点击右上角的“Fok”图标,稍 ...
- redis-4.0.2
redis-4.0.2.tar.gz 链接:https://pan.baidu.com/s/1qj4bSgM1s2InLikugRNqKA 提取码:tozq 复制这段内容后打开百度网盘手机App,操作 ...
- bootstrap switch样式修改与多列等间距布局
先以一张图开启今天的随笔 今天实习遇到了switch按钮,小姐姐说用插件bootstrap switch来写,我第一次用这个插件,首先在引入方面就遇到了很多坑,先来总结一下bootstrap swit ...
- 国密算法--Openssl 实现国密算法(加密和解密)
上一次讲了产生密钥,这次我们讲一下加密解密的实现. 先说一下加密解密的流程,一下这些内容都是从国密局发布的国密标准文档里面摘录出来的.大家可以去国密局的网站上自己下载. 下列符号适用于本部分. A,B ...
- Centos7 zabbix 自动发现与注册
自动发现与自动注册 自动发现: zabbix Server主动发现所有客户端,然后将客户端登记自己的小本上,缺点zabbix server压力山大(网段大,客户端多),时间消耗多. 自动注册: zab ...
- Python文本文件的输入输出操作学习
Python具有基本的文本文件读写功能.Python的标准库提供有更丰富的读写功能. 文本文件的读写主要通过open()所构建的文件对象来实现. 创建文件对象 我们打开一个文件,并使用一个对象来表示该 ...
- 让CentOS在同一个窗口打开文件夹
http://www.linuxidc.com/Linux/2010-04/25756.htm
- Java里字符串split方法
Java中的split方法以"."切割字符串时,需要转义 String str[] = s.split("\\.");
- Daily Scrumming 2015.10.21(Day 2)
今明两天任务表 Member Today’s Task Tomorrow’s Task 江昊 配置ruby与rails环境 配置mysql与数据库用户管理 配置apache2环境 学习rails Ac ...