题目链接: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. 实验三 kali下metasploit的漏洞攻击实践

    一.实验内容 1.使用kali进行靶机的漏洞扫描,利用metasploit选择其中的一个漏洞进行攻击,并获取权限. 2.分析攻击的原理以及获取了什么样的权限. 二.实验要求 1.熟悉kali原理和使用 ...

  2. 软件包管理器(bzoj 4196)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  3. KVM 网络虚拟化基础

    网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...

  4. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  5. 安卓解析XML文件

    安卓解析XML文件 主要有三种方式:DOM解析.SAX解析.PULL解析 其中: DOM解析为等XMl文件全部加载后,然后根据需要解析的内容解析出所需的内容数据. SAX解析为从XML文件中执行一行, ...

  6. poj1149最大流经典构图神题

    题意:n个顾客依次来买猪,有n个猪房,每个顾客每次可以开若干个房子,买完时,店主可以调整这位顾客 开的猪房里的猪,共m个猪房,每个猪房有若干猪,求最多能卖多少猪. 构图思想:顾客有先后,每个人想要的猪 ...

  7. 高性能mysql之schema与数据类型优化

    1.数据类型 http://www.cnblogs.com/YDDMAX/p/4937770.html

  8. centos6.5编译安装gearmand Job Server(C)

    1)下载安装包: wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz 2)安装编译器: yu ...

  9. python实现网速控制,限制上传下载速度

    对于python的web,比如flask使用的werkzeug,首先找到wsgi的请求和响应的代码,使用算法实现大文件的小速率上传和下载 考虑python实现socket限流 关于限速的讨论:http ...

  10. php 解决MySQL插入数据出现 Incorrect string value: &#39;\xF0\x9F\x92\x8BTi...&#39;错误

    在项目中向MySQL插入数据时.发现数据插入不完整,通过调试,发现插入语句也没什么特殊的错误. 可是就是差不进去,于是就打开mysqli错误的调试 $ret = mysqli_query($this- ...