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. P2239螺旋矩阵

    传送 看到这数据范围,显然咱不能暴力直接模拟(二维数组开不下,而且会T掉) 我们目前有两种选择: 1.优化暴力  走这边(jyy tql%%%) 2.数学做法 我们看一下题目中的那个矩阵 我们能不能找 ...

  2. ruby在类中访问@,类外访问调用方法

    class Box def initialize(w,h) @width,@height=w,h end def printWidth puts @width end def printHeight ...

  3. ruby中=>是什么意思

    如果是对数组赋值,下标 => 值例如 a = {1 => "1",2 => "22"}a[1] "1"a[2] " ...

  4. 138、Tensorflow serving 实现模型的部署

    将Tensorflow模型部署成Restful接口 下面是实现过程,整个操作都是在Linux上面实现的,因为Tensorflow Serving 目前还只支持Linux 这个意义真的是革命性的,因为从 ...

  5. maven添加oracle和sqlserver报错

    Failure to find com.oracle:ojdbc6:jar:12.1.0.1-atlassian-hosted in 'xxx' Missing artifact com.micros ...

  6. JS截取与分割字符串

    1.substr 方法 返回一个从指定位置开始的指定长度的子字符串. stringvar.substr(start [, length ]) start :必选项.所需的子字符串的起始位置.字符串中的 ...

  7. java包装类,自动装箱,拆箱,以及基本数据类型与字符串的转换

    package cn.learn; import java.util.ArrayList; /* 包装类 java.lang中,基本运算类型效率高 装箱:把基本类型数据包装为包装类 1.构造方法 In ...

  8. IDEA 光标显示注释

  9. 天堂Lineage(單機版)從零開始架設教學 Installing Lineage 3.52 Server - On Windows

      1. [下載原始碼] Using RapidSVN 用checkout      http://l1j-tw-99nets.googlecode.com/svn/trunk/L1J-TW_3.50 ...

  10. CLR 垃圾回收知识梳理