Pleasant sheep and big big wolf
题目:在一个N * M 的矩阵草原上,分布着羊和狼。每一个格子仅仅能存在0或1仅仅动物。如今要用栅栏将全部的狼和羊分开。问怎么放,栅栏数放的最少,求出个数?
解析:将狼群看作一个集合,羊群看作一个集合。然后设置源点和汇点,将两点至存在动物的点的距离赋值为1,构图,因为求得是栅栏数,从存在动物的位置向四周发散点赋值为1,即该方向放置一个栅栏。然后能够发现变成了求最小割,即求出最大流。须要注意的是,因为数据比較大,200 * 200。假设设置源点和汇点相差较大(即s = 0,e = n * m ),easyTLE.
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; // 最大流 ISAP + bfs+栈优化 const int maxn = 100010; //点的个数
const int maxm = 400010; //边的个数
const int INF = 0xfffffff; struct Edge{
int to, next, cap, flow;
}edge[ maxm ];//注意是maxm int tol;
int head[ maxn ];
int gap[ maxn ], dep[ maxn ], cur[ maxn ]; void init(){
tol = 0;
memset( head, -1, sizeof( head ) );
} void addedge( int u, int v, int w, int rw = 0 ){
edge[ tol ].to = v; edge[ tol ].cap = w; edge[ tol ].flow = 0;
edge[ tol ].next = head[ u ]; head[ u ] = tol++;
edge[ tol ].to = u; edge[ tol ].cap = rw; edge[ tol ].flow = 0;
edge[ tol ].next = head[ v ]; head[ v ] = tol++;
} int Q[ maxn ]; void BFS( int start, int end ){
memset( dep, -1, sizeof( dep ) );
memset( gap, 0, sizeof( gap ) );
gap[ 0 ] = 1;
int front = 0, rear = 0;
dep[ end ] = 0;
Q[ rear++ ] = end;
while( front != rear ){
int u = Q[ front++ ];
for( int i = head[ u ]; i != -1; i = edge[ i ].next ){
int v = edge[ i ].to;
if( dep[ v ] != -1 )
continue;
Q[ rear++ ] = v;
dep[ v ] = dep[ u ] + 1;
gap[ dep[ v ] ]++;
}
}
} int S[ maxn ];
int sap( int start, int end, int N ){
BFS( start, end );
memcpy( cur, head, sizeof( head ) );
int top = 0;
int u = start;
int ans = 0;
while( dep[ start ] < N ){
if( u == end ){
int Min = INF;
int inser;
for( int i = 0; i < top; ++i ){
if( Min > edge[ S[ i ] ].cap - edge[ S[ i ] ].flow ){
Min = edge[ S[ i ] ].cap - edge[ S[ i ] ].flow;
inser = i;
}
}
for( int i = 0; i < top; ++i ){
edge[ S[ i ] ].flow += Min;
edge[ S[ i ] ^ 1 ].flow -= Min;
}
ans += Min;
top = inser;
u = edge[ S[ top ] ^ 1 ].to;
continue;
}
bool flag = false;
int v;
for( int i = cur[ u ]; i != -1; i = edge[ i ].next ){
v = edge[ i ].to;
if( edge[ i ].cap - edge[ i ].flow && dep[ v ] + 1 == dep[ u ] ){
flag = true;
cur[ u ] = i;
break;
}
}
if( flag ){
S[ top++ ] = cur[ u ];
u = v;
continue;
}
int Min = N;
for( int i = head[ u ]; i != -1; i = edge[ i ].next ){
if( edge[ i ].cap - edge[ i ].flow && dep[ edge[ i ].to ] < Min ){
Min = dep[ edge[ i ].to ];
cur[ u ] = i;
}
}
gap[ dep[ u ] ]--;
if( !gap[ dep[ u ] ] )
return ans;
dep[ u ] = Min + 1;
gap[ dep[ u ] ]++;
if( u != start )
u = edge[ S[ --top ] ^ 1 ].to;
}
return ans;
} int dir[ ][ 2 ] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; int main(){
int N, M, Case = 1;
while( scanf( "%d%d", &N, &M ) != EOF ){
int s = N * M, t = N * M + 1;
int n = N * M + 2, temp;
init();
for( int i = 0; i < N; ++i ){
for( int j = 0; j < M; ++j ){
scanf( "%d", &temp );
if( temp == 2 ){
addedge( s, M * i + j, INF );
}
if( temp == 1 ){
addedge( M * i + j, t, INF );
}
for( int k = 0; k < 4; ++k ){
int x = i + dir[ k ][ 0 ];
int y = j + dir[ k ][ 1 ];
if( x >= 0 && x < N && y >= 0 && y < M )
addedge( M * i + j, M * x + y, 1 );
}
}
}
printf( "Case %d:\n%d\n", Case++, sap( s, t, n ) );
}
return 0;
}
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 wolf(最小割最大流+Dinic)
http://acm.hdu.edu.cn/showproblem.php?pid=3046 题意: 给出矩阵地图和羊和狼的位置,求至少需要建多少栅栏,使得狼不能到达羊. 思路:狼和羊不能到达,最小割 ...
- hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
题意: 给出最少栏杆数使狼和羊分离 分析: 将狼与源点连,羊与汇点连,容量都为无穷,将图的各个相邻点连接,容量为1 然后题目就转化成最小去掉多少条边使不连通,即求最小割最大流. // File Nam ...
- HDU 3046Pleasant sheep and big big wolf(切最小网络流)
职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...
- HDU3046_Pleasant sheep and big big wolf
给一个n*m的数字阵,1表示羊的位置,2表示狼的位置,0表示没有东西,可以通过.在每个格子的4边都可以建立围栏,有围栏的话狼是不能通过的. 现在求最少建立多少围栏能够保证狼无法接触到羊. 题目的模型很 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- vue懒加载实现
- ASP.NET--Attribute定义及用法
1.Attribute定义 公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microsoft .N ...
- jquery-常用插件集合
001.弹出消息插件toastr https://github.com/CodeSeven/toastr 002.弹出页面全屏插件 https://github.com/sindresorhus/sc ...
- maven规定的目录
Maven规定的目录结构 若要使用Maven,那么项目的目录结构必须符合Maven的规范 ,如写一个使用Spring的Web项目就需要引入大量的jar包.一个项目Jar包的数量之多往往让我们瞠目结舌, ...
- CSS3弹性布局内容对齐(justify-content)属性使用具体解释
内容对齐(justify-content)属性应用在弹性容器上.把弹性项沿着弹性容器的主轴线(main axis)对齐. 该操作发生在弹性长度以及自己主动边距被确定后. 它用来在存在剩余空间时怎样加以 ...
- elasticsearch的javaAPI之index
Index API 原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html ...
- [JZOJ 5909] [NOIP2018模拟10.16] 跑商(paoshang) 解题报告 (圆方树)
题目链接: https://jzoj.net/senior/#contest/show/2529/2 题目: 题目背景:尊者神高达很穷,所以他需要跑商来赚钱题目描述:基三的地图可以看做 n 个城市,m ...
- 搞笑OI
OI难 噫吁嚱,维护难哉!OI之难,难于上青天!哈希及DP,代码何茫然!尔来一千两百A,不见金牌背后难.西当华师有考场,可以横绝CN巅.编译不过壮士死,然后超时爆内存相钩连.上有自主招生之高标,下有由 ...
- Laravel-HTTP-验证
Laravel-HTTP-验证 标签(空格分隔): php 第一种方式 **1 直接在controller里完成表单验证** **2 打印验证返回的错误信息 dd($errors)** 第二种方式 * ...
- POJ 1951 模拟
思路: 坑爹模拟毁我一生 给两组数据: 输入: YOURE TRAVELING THROUGH ANOTHER DIMENSION A DIMENSION NOT OF SIGHT. 输出: YR T ...