题目链接:https://vjudge.net/problem/HDU-2732

Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3231    Accepted Submission(s): 1326

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.
 
Source
 
Recommend
zty

题意:

在一个n*m的地图上, 有一些高度不一柱子, 又有一些青蛙站在柱子上,且一根柱子最多只能站一只青蛙。青蛙一次最多可跳跃d个距离,即:abs(x-xx)+abs(y-yy)<=d,且每跳一次,青蛙原来站着的柱子的高度会下降一个单位(作用力与反作用力?),当柱子的高度为0时,就无效了。当青蛙跳出界时,才算安全,问:最少有多少个青蛙不能跳出地图?

题解:

可用网络流建模求解,建图方法如下:

1.将每个柱子拆成两个点u、u',u用于跳入,u'用于跳出,且连一条边:u-->u',容量为高度,以限制最大跳跃次数。

2.设置超级源点,如果一根柱子上有青蛙,那么从超级源点向该柱子连一条边,容量为1,表明有一只青蛙。

3.设置超级汇点,如果从某根柱子上能够一步跳出地图,那么就从该柱子往超级汇点连一条边,容量为该柱子的高度(容量),表明从这根柱子跳出界的青蛙最多能有多少只。

4.如果u柱子能跳到v柱子,那么就连一条边u-->v,容量为u柱子的高度(容量),表明从u柱子最多能有多少只青蛙跳到v柱子。

5.跑最大流算法,所求得的就是能跳出地图的最大青蛙数,再用总的青蛙数减之,就是答案。

领接矩阵:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN]; int sap(int start, int end, int nodenum)
{
memset(cur, , sizeof(cur));
memset(dis, , sizeof(dis));
memset(gap, , sizeof(gap));
memset(flow, , sizeof(flow));
int u = pre[start] = start, maxflow = , aug = INF;
gap[] = nodenum; while(dis[start]<nodenum)
{
loop:
for(int v = cur[u]; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && dis[u] == dis[v]+)
{
aug = min(aug, maze[u][v]-flow[u][v]);
pre[v] = u;
u = cur[u] = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u, u = pre[u])
{
flow[u][v] += aug;
flow[v][u] -= aug;
}
aug = INF;
}
goto loop;
} int mindis = nodenum-;
for(int v = ; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && mindis>dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]])==) break;
gap[dis[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} int T, n, m, d;
bool inbroad(int x, int y)
{
return (x>= && x<n && y>= && y<m);
} char pillar[][], lizard[][];
int id[][], pnum, lnum;
int main()
{
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &d);
for(int i = ; i<n; i++) scanf("%s", pillar[i]);
for(int i = ; i<n; i++) scanf("%s", lizard[i]);
m = strlen(pillar[]); pnum = ; lnum = ;
for(int i = ; i<n; i++)
for(int j = ; j<m; j++)
{
if(lizard[i][j]=='L') lnum++;
if(pillar[i][j]-'') id[i][j] = pnum++;
} int start = *pnum, end = *pnum+, N = *pnum+;
memset(maze, , sizeof(maze));
for(int i = ; i<n; i++)
for(int j = ; j<m; j++)
{
int cap = pillar[i][j]-'';
if(cap)
{
if(lizard[i][j]=='L') maze[start][id[i][j]] = ;
maze[id[i][j]][pnum+id[i][j]] = cap;
bool flag = false;
for(int xd = -d; xd<=d; xd++) //枚举横坐标方向
for(int yd = abs(xd)-d; yd<=d-abs(xd); yd++) //枚举纵坐标方向
{
if(inbroad(i+xd, j+yd) && (pillar[i+xd][j+yd]-'')) maze[pnum+id[i][j]][id[i+xd][j+yd]] = cap;
if(!inbroad(i+xd, j+yd)) flag = true;
}
if(flag) maze[pnum+id[i][j]][end] = cap;
}
} int left = lnum - sap(start, end, N);
if(left==) printf("Case #%d: no lizard was left behind.\n", kase);
else if(left==) printf("Case #%d: 1 lizard was left behind.\n", kase);
else printf("Case #%d: %d lizards were left behind.\n", kase, left);
}
}

邻接表:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; struct Edge
{
int to, next, cap, flow;
}edge[MAXM];
int tot, head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int sap(int start, int end, int nodenum)
{
memset(dep, , sizeof(dep));
memset(gap, , sizeof(gap));
memcpy(cur, head, sizeof(head));
int u = pre[start] = start, maxflow = ,aug = INF;
gap[] = nodenum;
while(dep[start]<nodenum)
{
loop:
for(int i = cur[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+)
{
aug = min(aug, edge[i].cap-edge[i].flow);
pre[v] = u;
cur[u] = i;
u = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u,u = pre[u])
{
edge[cur[u]].flow += aug;
edge[cur[u]^].flow -= aug;
}
aug = INF;
}
goto loop;
}
}
int mindis = nodenum;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap-edge[i].flow && mindis>dep[v])
{
cur[u] = i;
mindis = dep[v];
}
}
if((--gap[dep[u]])==)break;
gap[dep[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} int T, n, m, d;
bool inbroad(int x, int y)
{
return (x>= && x<n && y>= && y<m);
} char pillar[][], lizard[][];
int id[][], pnum, lnum;
int main()
{
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &d);
for(int i = ; i<n; i++) scanf("%s", pillar[i]);
for(int i = ; i<n; i++) scanf("%s", lizard[i]);
m = strlen(pillar[]); pnum = ; lnum = ;
for(int i = ; i<n; i++)
for(int j = ; j<m; j++)
{
if(lizard[i][j]=='L') lnum++;
if(pillar[i][j]-'') id[i][j] = pnum++;
} int start = *pnum, end = *pnum+, N = *pnum+;
init(); for(int i = ; i<n; i++)
for(int j = ; j<m; j++)
{
int cap = pillar[i][j]-'';
if(cap)
{
if(lizard[i][j]=='L') add(start, id[i][j], );
add(id[i][j], pnum+id[i][j], cap);
bool flag = false;
for(int xd = -d; xd<=d; xd++) //枚举横坐标方向
for(int yd = abs(xd)-d; yd<=d-abs(xd); yd++) //枚举纵坐标方向
{
if(inbroad(i+xd, j+yd) && (pillar[i+xd][j+yd]-'')) add(pnum+id[i][j], id[i+xd][j+yd], cap);
if(!inbroad(i+xd, j+yd)) flag = true;
}
if(flag) add(pnum+id[i][j], end, cap);
}
} int left = lnum - sap(start, end, N);
if(left==) printf("Case #%d: no lizard was left behind.\n", kase);
else if(left==) printf("Case #%d: 1 lizard was left behind.\n", kase);
else printf("Case #%d: %d lizards were left behind.\n", kase, left);
}
}

HDU2732 Leapin' Lizards —— 最大流、拆点的更多相关文章

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

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

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

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

  3. hdu 2732 Leapin' Lizards (最大流 拆点建图)

    Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...

  4. HDU2732 Leapin' Lizards 最大流

    题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...

  5. hdu2732 Leapin' Lizards (网络流dinic)

    D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  6. HDU2732 Leapin' Lizards

    Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU2732 Leapin' Lizards 网络流 最大流 SAP

    原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...

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

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

  9. HDU2732:Leapin' Lizards(最大流)

    Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. Laravel 5 Form 和 HTML 的使用

    最近在用 laravel 5 做例子,在做到表单的时候,习惯性的使用 Form::open() 结果发现提示错误,没有这个类, 好吧,找了找,发现 在laravel 5 中,把 from 和 html ...

  2. 云计算与 OpenStack

    “云计算” 算是近年来最热的词了.现在 IT 行业见面不说这三个字您都不好意思跟人家打招呼. 对于云计算,学术界有各种定义,大家有兴趣可以百度一下. CloudMan 这里主要想从技术的角度谈谈对云计 ...

  3. 【BZOJ2002】弹飞绵羊(LCT)

    题意:给定一棵树,要求维护以下操作: 1.删除连接(x,y)的边 2.将(x,y)之间连边 3.询问某点子树大小 对于100%的数据n<=200000,m<=100000 思路:第一道有加 ...

  4. ci框架——文章查看之上篇下篇

    1:从数据库查询出推荐的文章的信息,循环查出每篇推荐文章的id和title; foreach($data as $val){ $dataid[]=$val->aid; $datatitle[]= ...

  5. python和python-dev

    问:python-dev是什么?为什么安装了python后有时还要安装python-dev? 答: linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el ...

  6. gorm 结构体 预加载

    结构体构建 type PlansApproval struct {     ID uint     Plans_Id int //plans编号     UpdateUser int //更新者    ...

  7. 内核调试 SystemTap

    http://www.cnblogs.com/wangkangluo1/archive/2012/06/26/2562971.html   相关技术:utrace, probe, ftrace, dt ...

  8. mysql 隔离级别与间隙锁等

    数据库隔离级 SQL标准中DB隔离级别有: read uncommitted:可以读到其它transaction 未提交数据 read committed:可以读到其它transaction 已提交数 ...

  9. Xib/Storyboard碰到不同版本的Xcode真是想死啊!

    Command /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/ ...

  10. 完整的MVC框架(前端、后台和数据库)

    终于学完了数据库的连接,可以做一个完整的项目了,以前做的练习都没有关联到数据库,没法进行事务. MVC框架 先上图: 老师画的图,有点乱,但是大概意思还是可以理解. 这个练习是简单的存储一个学生读了哪 ...