HDU2732 Leapin' Lizards —— 最大流、拆点
题目链接: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
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.
always 1 ≤ d ≤ 3.
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..
........
........
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.
题意:
在一个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 —— 最大流、拆点的更多相关文章
- hdu2732 Leapin' Lizards 最大流+拆点
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...
- hdu 2732 Leapin' Lizards 最大流 拆点 建图
题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...
- hdu 2732 Leapin' Lizards (最大流 拆点建图)
Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...
- HDU2732 Leapin' Lizards 最大流
题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...
- hdu2732 Leapin' Lizards (网络流dinic)
D - Leapin' Lizards Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU2732 Leapin' Lizards
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU2732 Leapin' Lizards 网络流 最大流 SAP
原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...
- HDU-2732-leapin'Lizards(最大流, 拆点)
链接: https://vjudge.net/problem/HDU-2732 题意: Your platoon of wandering lizards has entered a strange ...
- HDU2732:Leapin' Lizards(最大流)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- cf682E Alyona and Triangles
You are given n points with integer coordinates on the plane. Points are given in a way such that th ...
- ubuntu mysql安装及需要其他主机连服务器mysql时的设置(error:10061)
说明: 一个朋友在使用ubuntu-server 16.04安装mysql,设置远程访问的时候出现了问题,请我帮忙.但是,我也没有使用过ubuntu安装mysql,于是乎搜索了很多技术文件,比着葫芦画 ...
- Scrapy学习-6-JSON数据处理
使用json模块处理JSON数据 class JsonwithEncodingPipeline(object): def __init__(self): self.file = codecs.open ...
- (5)Swing事件
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; publi ...
- [BOI2007] Mokia
题目描述 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它能够回 ...
- Ubuntu 16.04安装JAD反编译工具(Java)
JAD反编译工具有个好处,就是字节码和源代码一起输出. 官网:https://varaneckas.com/jad/ 安装步骤: 1.下载: 离线版本:(链接: https://pan.baidu.c ...
- yum安装nginx详解
原文:http://blog.csdn.net/tjcyjd/article/details/50686505 1.查看yum的nginx信息 # yum info nginx Loaded plug ...
- 使用Myeclipse + SVN + TaoCode 免费实现项目版本控制的详细教程
通过Myeclipse + SVN插件 + TaoCOde可以省去代码仓库的租建:同时还可以很好的满足小团队之间敏捷开发的需求.接下来详细介绍整个搭建流程. 首先,介绍所用到的工具: 1,Myecli ...
- 【paddle学习】图像分类
https://zhuanlan.zhihu.com/p/28871960 深度学习模型中的卷积神经网络(Convolution Neural Network, CNN)近年来在图像领域取得了惊人的成 ...
- BUPT复试专题—密码(2009)
题目描述 输入 有多组输入,每组: 第一行:由26个小写字母组成的串以空格隔开,如 b a c e u f g h i j k l m n o p q r s t v w x y z d v y ...