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 ...
随机推荐
- day1之校花网小试牛刀
一 利用生成器来完成爬去校花网视频 import requests import re import os import hashlib import time DOWLOAD_PATH=r'D:\D ...
- POJ 2411 状压dp
F - Mondriaan's Dream Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I6 ...
- 让Mac OS X下的终端像Linux那样拥有丰富多彩的颜色显示
我们知道Linux下的命令行终端具有颜色回显功能,用ls命令查看目录或者文件,终端会以不同的颜色来区分:使用vim命令行编辑器打开脚本或其他源程序代码会以语法高亮模式显示.而Mac OS X下的终端却 ...
- css,世界上没有绝对简单的事情
引文 自从学了前端的基础,自认为是没什么css是能难倒我的,可是事实是,世界上没有绝对简单的事情,实际上还有好多的东西等待我们去发掘. 详解 1.有些浏览器不完全支持css3,现在可以用 modern ...
- windows 80端口占用情况查询
在开始-运行,输入CMD打开命令行界面,输入命令 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 具体对应 ...
- 使用git 高效多人合作
复习一下... 附加学习链接: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/) ...
- Go语言并发之美
简介 多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...
- [React] Create and import React components with Markdown using MDXC
In this lesson I demonstrate how to use the library MDXC to create and import React components with ...
- SolidEdge如何为零件指定不同的颜色 给零件着色 给装配体着色
格式-零件画笔 可以给零件的一个表面,或者一个特征着色 如果要指定不同的色彩,可以在格式-样式-面样式中修改 如果是给装配体着色,则点击任意零件,在"无"的选项卡里面修改颜 ...
- 使用RPi-Monitor监控、统计Guitar的运行状态
前言 之前发在ickey社区上的一系列文章: 犹抱琵琶半遮面,无人知是荔枝来--unboxing & interview 一.二.三 葡萄美酒夜光杯,巧妇难为无米炊--资料与社区 一支穿云箭, ...