hdu 2732 Leapin' Lizards (最大流 拆点建图)
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.
建图:
源点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(列号之差)
最终我们求出的最大流就是能跳出网格的蜥蜴数.
#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 (最大流 拆点建图)的更多相关文章
- hdu 2732 Leapin' Lizards 最大流 拆点 建图
题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...
- HDU 2732 Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...
- HDU 2732 Leapin' Lizards
网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...
- hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...
- hdu2732 Leapin' Lizards 最大流+拆点
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...
- HDU 2732 Leapin' Lizards(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- HDU-2732-leapin'Lizards(最大流, 拆点)
链接: https://vjudge.net/problem/HDU-2732 题意: Your platoon of wandering lizards has entered a strange ...
随机推荐
- 题解1433. 数码问题 (Standard IO)
Description Alice有一个N*N的格子,把1-N^2按照从上到下从左到右的顺序填进表格中,允许在表格上进行两种操作: (1) 旋转行——这一行的数向右移动一个位置,而最后一列的数会移到第 ...
- Git003--创建版本库
Git--创建版本库 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...
- Mac003--Maven安装与环境变量配置
Mac--Maven安装 一.应用brew安装maven及安装位置 打开终端,输入命令:brew install maven 参考博客:https://www.jianshu.com/p/230e0b ...
- python数据类型补充
四.元组 #为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,3],'sss',(1,2)) #t=tuple((1,[1,3],'sss',(1,2))) # print(t ...
- eclipse的maven配置及本地仓库配置
一.下载maven并解压 下载地址:http://maven.apache.org/download.cgi 解压后如下: 二.配置环境变量 配置MAVEN_HOME 再path中添加 安装成功 三. ...
- 16、NumPy ——字节交换
NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...
- python 丰田经销商
import requests import json from dbutil.pgsql import PgsqlPipeline from datetime import date headers ...
- [fw]Real Mode addressing
Real Mode 在 real mode 中,memory 的使用被限制在 1 MByte(220 bytes) 內,可用的 address 範圍為 0x00000 ~ 0xFFFFF. 由 mem ...
- Little Sub and Mr.Potato's Math Problem (构造法)
题目传送门Little Sub and Mr.Potato's Math Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Littl ...
- JVM(16)之 双亲委派模型
开发十年,就只剩下这套架构体系了! >>> 在上一篇博文中,我们知道了如何获得二进制的字节流,并根据获得的字节流去装载一个类.同时也了解到类加载器的存在,每个加载器对应着不同的加 ...