poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流

时间:2022-10-24 23:25:39

题目链接:http://poj.org/problem?id=2711

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066

Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below...

Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.

The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.

题意描述:在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个石柱上。

算法分析:新增源点from和汇点to,每个位置u拆边u->r*c+u(如果位置上没石柱,权值为inf,否则权值为石柱的高度);石柱之间如果距离不超过D,连边并权值为无穷大;

from连边有蜥蜴的位置(点拆分后的前驱点,权值为1);如果点的位置距离边界外不超过D,则该点拆分后的后继点连边汇点to,权值为inf。最大流解之即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int r,c,from,to;
int D;
struct node
{
int v,flow;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ; edge[edgenum].v=u ;edge[edgenum].flow= ;
edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(edge[i].flow,cap));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int sum=;
while (bfs()) sum += dfs(from,inf);
return sum;
} int main()
{
char str[][];
char str2[][];
int ncase=;
int t;
scanf("%d",&t);
while (t--)
{
memset(str,,sizeof(str));
scanf("%d%d",&r,&D);
for (int i= ;i<=r ;i++)
scanf("%s",str[i]+);
c=strlen(str[]+);
memset(head,-,sizeof(head));
edgenum=;
from=*r*c+;
to=from+;
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
if (str[i][j]!='')
{
for (int u= ;u<=r ;u++)
{
for (int v= ;v<=c ;v++)
{
if (u==i && v==j) continue;
if (str[u][v]=='') continue;
int dis=abs(u-i)+abs(v-j);
if (dis<=D) {
add(r*c+(i-)*c+j,(u-)*c+v,inf);
}
}
}
}
}
memset(str2,,sizeof(str2));
for (int i= ;i<=r ;i++) scanf("%s",str2[i]+);
int ans=;
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
{
if (str2[i][j]=='.') continue;
ans ++ ;
add(from,(i-)*c+j,);
}
}
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
{
int num=str[i][j]-'';
add((i-)*c+j,r*c+(i-)*c+j,num== ? inf : num);
if (i<=D || j<=D || (r-i)<=D- || (c-j)<=D-)
add(r*c+(i-)*c+j,to,inf);
}
}
//cout<<ans<<endl;
int sum=ans-dinic();
printf("Case #%d: ",ncase++);
if (sum==) printf("no lizard was left behind.\n");
else if (sum==) printf("1 lizard was left behind.\n");
else printf("%d lizards were left behind.\n",sum);
}
return ;
}

poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流的更多相关文章

  1. POJ 2711 Leapin&&num;39&semi; Lizards &sol; HDU 2732 Leapin&&num;39&semi; Lizards &sol; BZOJ 1066 &lbrack;SCOI2007&rsqb;蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

  2. BZOJ 1066&colon; &lbrack;SCOI2007&rsqb;蜥蜴&lpar; 最大流 &rpar;

    结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...

  3. &lbrack;BZOJ 1066&rsqb; &lbrack;SCOI2007&rsqb; 蜥蜴 【最大流】

    题目链接:BZOJ - 1066 题目分析 题目限制了高度为 x 的石柱最多可以有 x 只蜥蜴从上面跳起,那么就可以用网络流中的边的容量来限制.我们把每个石柱看作一个点,每个点拆成 i1, i2,从 ...

  4. BZOJ 1066 &lbrack;SCOI2007&rsqb;蜥蜴(最大流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1066 [题目大意] 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些 ...

  5. bzoj 1066&colon; &lbrack;SCOI2007&rsqb; 蜥蜴

    这道题还是挺好想的,但我一开始还是想错了…… 把每个石柱拆成两个点,一个入度,一个出度,两个点连一条容量为高度的边,这样就可以限制从此石柱上经过的蜥蜴的数量.关于蜥蜴是否单独成点,我是单独当成了一个点 ...

  6. POJ - 2711 Leapin&&num;39&semi; Lizards

    题目大意: 在一个网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一 ...

  7. bzoj 1066 &colon; &lbrack;SCOI2007&rsqb;蜥蜴 网络流

    题目链接 给一个n*m的图, 里面每一个点代表一个石柱, 石柱有一个高度. 初始时有些石柱上面有蜥蜴, 蜥蜴可以跳到距离他曼哈顿距离小于等于d的任意一个石柱上,跳完后, 他原来所在的石柱高度会减一, ...

  8. 1066&colon; &lbrack;SCOI2007&rsqb;蜥蜴

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3545  Solved: 1771[Submit][Status] ...

  9. &lbrack;SCOI2007&rsqb; 蜥蜴 &lpar;最大流&rpar;

    [SCOI2007] 蜥蜴 题目背景 07四川省选 题目描述 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1 ...

随机推荐

  1. 我的ORM汇总

    MyOql是我写的ORM,目前仅支持 MSSql2005+ ,从2009年到今天,已使用过不少项目,之后会写 其它关系数据库的解析器: MySql,Sqlite,Oracle 等. 代码地址(最新版) ...

  2. Windows平台下利用APM来做负载均衡方案 - 负载均衡(下)

    概述 我们在上一篇Windows平台分布式架构实践 - 负载均衡中讨论了Windows平台下通过NLB(Network Load Balancer) 来实现网站的负载均衡,并且通过压力测试演示了它的效 ...

  3. java24

    1:多线程(理解)    (1)JDK5以后的针对线程的锁定操作和释放操作        Lock锁    (2)死锁问题的描述和代码体现    (3)生产者和消费者多线程体现(线程间通信问题)   ...

  4. iOS开发-UISlider改变图片透明度

    拖动条是通过滑块的位置来标识数值,而且拖动条允许用户拖动滑块来改变值.因此,拖动条通常用于对系统的某种数值进行调节,如调节亮度,透明度,音量等. 一.属性介绍 @property(nonatomic) ...

  5. (转发)RequestDispatcher的include&lpar;&rpar;方法和forward&lpar;&rpar;方法的区别

    forward(): 该方法用于将请求从一个Servlet传递给服务器上的另外的Servlet.JSP页面或者是HTML文件. 在Servlet中,可以对请求做一个初步的处理,然后调用这个方法,将请求 ...

  6. Python内置函数&lpar;51&rpar;——hasattr

    英文文档: hasattr(object, name) The arguments are an object and a string. The result is True if the stri ...

  7. python 递归实现汉诺塔算法

    def move(n,a,b,c): if (n == 1): print ( "第 ", n ," 步: 将盘子由 " ,a ," 移动到 &quo ...

  8. mysql原理~创建用户的那些事情

    一 简介:mysql是如何创建用户的二 基本语法:  1 grant 权限 on db.table to 'user'@'ip' identified by 'password'     目的 创建用 ...

  9. 采用busybox 代替android 自带的shell

    折腾了几天,被Android那点儿少得可怜的shell命令折磨的死去活来,终于下定了革命的决心.看一下怎么把渺小的toolbox替换成伟大的busybox吧.先大致描述一下Android系统中的she ...

  10. 论文笔记:ReNet&colon; A Recurrent Neural Network Based Alternative to Convolutional Networks

    ReNet: A Recurrent Neural Network Based Alternative to Convolutional Networks2018-03-05  11:13:05   ...