Problem Description
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.
 
Input
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.
 Output
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.
 Sample Input
4
3 1
1111
1111
1111
LLLL
LLLL
LLLL
3 2
00000
01110
00000
.....
.LLL.
.....
3 1
00000
01110
00000
.....
.LLL.
.....
5 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........
 
Sample Output
Case #1: 2 lizards were left behind.
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.
 
题意:给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d
如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x
即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

建图:

源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)

如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)

最终我们求出的最大流就是能跳出网格的蜥蜴数.

 
网络流的拆点操作是为了解决有些题目要求每个点的选择次数是有限制的.
比如 id 这个点的被选择次数最大是3,那么我们把它拆成id1 跟 id2两个点分别来表示入id和出id
再在id1和id2之间建一条容量为3的边,这样每次进入id的时候我们就可以让它其实进入id1然后原地跳一下跳到id2这样就控制了某个点的选择的次数
 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
char s[][];
char ss[][];
struct Edge
{
int from,to,cap,flow;
Edge (){}
Edge (int f,int t,int c,int fl){from=f,to=t,cap=c,flow=fl;}
};
struct Dinic
{
int n,m,s,t;
vector <Edge> edges;
vector <int> G[maxn];
int cur[maxn];
int dep[maxn];
bool vis[maxn];
void init (int n,int s,int t)
{
this->n=n;this->s=s;this->t=t;
edges.clear();
for (int i=;i<n;++i)
G[i].clear();
}
void addedge (int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs ()
{
queue <int> q;
while (!q.empty()) q.pop();
memset(vis,false,sizeof vis);
vis[s] = true;
dep[s] = ;
q.push(s);
while (!q.empty()){
int u = q.front();
q.pop();
for (int i=;i<G[u].size();++i){
Edge e = edges[G[u][i]];
int v = e.to;
if(!vis[v]&&e.cap>e.flow){
vis[v] = true;
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return vis[t];
}
int dfs (int x,int mi){
if (x==t||mi==) return mi;
int flow = ,f;
for (int &i=cur[x];i<G[x].size();++i){
Edge &e = edges[G[x][i]];
int y = e.to;
if (dep[y]==dep[x]+&&(f=dfs(y,min(mi,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
mi-=f;
if (mi==) break;
}
}
return flow;
}
int max_flow ()
{
int ans = ;
while (bfs()){
memset(cur,,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
}dinic;
int full_flow;
bool check (int x,int y,int i,int j,int d)
{
if (abs(x-i)+abs(y-j)<=d) return true;
else return false;
}
bool out (int x,int y,int d)
{
if (x-d) return true;
else return false;
}
int main()
{
//freopen("de.txt","r",stdin);
int casee = ;
int T;
scanf("%d",&T);
while (T--){
int n,m,src,dst,d;
full_flow = ;
scanf("%d%d",&n,&d);
for (int i=;i<=n;++i)
scanf("%s",s[i]+);
int len = strlen(s[]+);
m = len;
src = ; dst = *n*m+;
dinic.init(*n*m+,src,dst);
for (int i=;i<=n;++i){
for (int j=;j<=len;++j){
if (s[i][j]-''>){
int id = (i-)*m+j;
dinic.addedge(id,id+n*m,s[i][j]-'');
if (i<=d || i+d>n || j<=d || j+d>m){//这个点能直接跳出去
dinic.addedge(id+n*m,dst,inf);
}
else{
for (int x=;x<=n;++x){
for (int y=;y<=m;++y){
if (x==i&&y==j) continue;
if (check(x,y,i,j,d)){
int id2 = (x-)*m+y;
dinic.addedge(id+n*m,id2,inf);//这个点的出连向能到达点的入
//dinic.addedge(id2+n*m,id,inf);
}
}
}
}
} }
}
for (int i=;i<=n;++i){
scanf("%s",ss[i]+);
for (int j=;j<=len;++j){
if (ss[i][j]=='L'){
full_flow++;
int id = (i-)*m+j;
dinic.addedge(src,id,);
}
}
}
int ans = full_flow-dinic.max_flow();
if(ans==) printf("Case #%d: no lizard was left behind.\n",++casee);
else if(ans==) printf("Case #%d: 1 lizard was left behind.\n",++casee);
else printf("Case #%d: %d lizards were left behind.\n",++casee,ans);
}
return ;
}
 

hdu 2732 Leapin' Lizards (最大流 拆点建图)的更多相关文章

  1. hdu 2732 Leapin' Lizards 最大流 拆点 建图

    题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...

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

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

  3. HDU - 2732 Leapin' Lizards (拆点最大流)

    题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...

  4. HDU 2732 Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...

  5. HDU 2732 Leapin' Lizards

    网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...

  6. hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005

    废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...

  7. hdu2732 Leapin' Lizards 最大流+拆点

    Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...

  8. HDU 2732 Leapin&#39; Lizards(拆点+最大流)

    HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...

  9. HDU-2732-leapin'Lizards(最大流, 拆点)

    链接: https://vjudge.net/problem/HDU-2732 题意: Your platoon of wandering lizards has entered a strange ...

随机推荐

  1. 【CDN+】Kafka 的初步认识与入门

    前言 项目中用到了Kafka 这种分布式消息队列来处理日志,本文将对Kafka的基本概念和原理做一些简要阐释 Kafka 的基本概念 官网解释: Kafka是最初由Linkedin公司开发,是一个分布 ...

  2. vue-slot的使用

    父组件在子组件内套的内容,是不显示的:vue有一套内容分发的的API,<slot>作为内容分发的出口,假如父组件需要在子组件内放一些DOM,那么这些DOM是显示.不显示.在哪个地方显示.如 ...

  3. CVTRES : fatal error CVT1100: duplicate resource

    升级某些VC6工程到VS2017,除了目录问题外,就是这个. 解决方法: Properties > Linker > Manifest File 第一项,Generate Manifest ...

  4. mooc-IDEA 编写高质量代码--009

    十五.IntelliJ IDEA -编写高质量代码 1.重构 [1]重构变量 选中某个变量,按住 shift+F6,修改变量名,则所有该变量名均会被重构为新变量名 [2]重构方法[ctrl+F6 | ...

  5. python 爬虫之requests爬取页面图片的url,并将图片下载到本地

    大家好我叫hardy 需求:爬取某个页面,并把该页面的图片下载到本地 思考: img标签一个有多少种类型的src值?四种:1.以http开头的网络链接.2.以“//”开头网络地址.3.以“/”开头绝对 ...

  6. 记一次Laravel 定时任务schedul:run未执行的处理

    关于Laravel的任务调度(定时任务)的配置在此不做赘述,跟着官方文档一步一步的操作是不会导致定时任务不能正常工作的. 为保证能及时捕获定时任务执行出现异常的原因,只需在配置系统crontab时指定 ...

  7. 关于Object.create()与原型链的面试题?

    原文地址 https://segmentfault.com/q/1010000004670616?utm_source=weekly&utm_medium=email&utm_camp ...

  8. [BZOJ4151]The Cave

    Solution: ​ 假设现在在点1,有许多形如 (x, y, z) 的限制条件,那么对于一组限制,必须先走到 x, y 的 \(\frac{z-dis(x, y)}{2}\) 级祖先,叫这些点为限 ...

  9. POJ1742 coins 动态规划之多重部分和问题

    原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...

  10. 让 Git Bisect 帮助你

    让 Git Bisect 帮助你 英文原文:Letting Git Bisect Help You   Git 提供来很多的工具来帮助我们改进工作流程. bisect 命令就是其中之一, 虽然由于使用 ...