题目链接: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' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

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

  2. BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )

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

  3. [BZOJ 1066] [SCOI2007] 蜥蜴 【最大流】

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

  4. BZOJ 1066 [SCOI2007]蜥蜴(最大流)

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

  5. bzoj 1066: [SCOI2007] 蜥蜴

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

  6. POJ - 2711 Leapin' Lizards

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

  7. bzoj 1066 : [SCOI2007]蜥蜴 网络流

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

  8. 1066: [SCOI2007]蜥蜴

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

  9. [SCOI2007] 蜥蜴 (最大流)

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

随机推荐

  1. event.preventDefault()

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. javascript组件化(转)

    javascript组件化(转) By purplebamboo 3月 16 2015 更新日期:3月 23 2015 文章目录 1. 最简陋的写法 2. 作用域隔离 3. 面向对象 4. 抽象出ba ...

  3. 在xml中调用自己用java代码定义的View

    1.在res中new一个class继承view.View,重写ondraw方法,写出自己的view package com.zzw.myView; import android.content.Con ...

  4. Delphi开发的IP地址修改工具

    用Delphi进行开发的,直接修改注册表,需重启电脑后才生效

  5. 扯一扯前端css的整体架构设计:(2)base基础类的那些事儿

    周一下午在实验室写了第一篇博文,有几个人捧场,那咱就得接着下去啊.然后我觉得现在写的内容更多的偏向于谈一下我对于前端css架构的理解和前端经验的一个小总结,所以就把标题里原来的[项目总结]给删掉了.但 ...

  6. WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)

    无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...

  7. wpf 在引用外部的资源字典

    启动的APP.xaml

  8. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

  9. Android WIFI 启动流程

    参考:http://blog.chinaunix.net/uid-26215986-id-3260413.html 一. WIFI 工作步骤 1. Wifi模块初始化 2. Wifi启动 3. 查找热 ...

  10. hdu 4006/AvlTree

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 这道题以前用c语言写的Avltree水过了.. 现在接触了c++重写一遍... 由于没有删除操作 ...