hdu2732 Leapin' Lizards (网络流dinic)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
always 1 ≤ d ≤ 3.
Output
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int maxn = ;
int cas = ; struct Edge{
int from,to,cap,flow;
Edge() {}
Edge(int a,int b,int c,int d)
{
from=a,to=b,cap=c,flow=d;
}
}; struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
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-);
}
void init(int x)
{
memset(d,,sizeof(d));
edges.clear();
for(int i=;i<=x;i++)
G[i].clear();
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow = , f;
for(int &i=cur[x];i<G[x].size();i++)
{
Edge &e=edges[G[x][i]];
if(d[x]+==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow = ;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}; Dinic dinic;
int n,m,d;
char g1[][],g2[][];
inline int id_p(int x,int y) {return (x*m+y)*;}
inline int id_l(int x,int y) {return x*m+y+;}
inline bool inside(int x,int y) {return x>= && x<=n && y>= && y<=m;}
int s = , t = ;
inline bool canout(int i,int j)
{
// cout<<i<<' '<<j<<endl;
for(int x=i-d;x<=i+d;x++)
for(int y=j-d;y<=j+d;y++)
{
if(abs(x-i)+abs(y-j)>d || (x==i && y==j)) continue;
if(!inside(x,y)) return ;
}
return ;
}
void run()
{
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
scanf("%s",g1[i]+);
for(int i=;i<=n;i++)
scanf("%s",g2[i]+);
m=strlen(g1[]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
g1[i][j]-='';
dinic.init(t);
int sum = ;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(g1[i][j]==) continue;
int u1 = id_p(i,j);
int u2 = u1^;
dinic.AddEdge(u1,u2,g1[i][j]);
for(int x=i-d;x<=i+d;x++)
for(int y=j-d;y<=j+d;y++)
{
if(abs(x-i)+abs(y-j)>d || (x==i && y==j)) continue;
if(!inside(x,y))
{
dinic.AddEdge(u2,t,g1[i][j]);
goto bk;
}
else
{
dinic.AddEdge(u2,id_p(x,y),g1[i][j]);
}
}
bk:;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(g2[i][j]!='L') continue;
if(g1[i][j]== && canout(i,j)) continue;
sum++;
dinic.AddEdge(s,id_l(i,j),);
dinic.AddEdge(id_l(i,j),id_p(i,j),);
}
int ans = sum - dinic.Maxflow(s,t);
printf("Case #%d: ",cas++); //cout<<sum<<' ';
if(ans==) puts("no lizard was left behind.");
else if(ans==) puts("1 lizard was left behind.");
else printf("%d lizards were left behind.\n",ans);
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}
hdu2732 Leapin' Lizards (网络流dinic)的更多相关文章
- HDU2732 Leapin' Lizards 网络流 最大流 SAP
原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...
- HDU2732 Leapin' Lizards —— 最大流、拆点
题目链接:https://vjudge.net/problem/HDU-2732 Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU2732 Leapin' Lizards
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu2732 Leapin' Lizards 最大流+拆点
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...
- HDU2732 Leapin' Lizards 最大流
题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- HDU2732:Leapin' Lizards(最大流)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Leapin' Lizards
Leapin' Lizards 题目大意: 在一个网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴 ...
随机推荐
- python 基础 3.1 打开文件 a a+ r+ w+ 详解
一.python 访问文件 1.在python中要访问文件,首先要打开文件,也就是open ---open r: 只读 w: 只写 ,文件已存在则清空,不存在则创建 a:追加 ...
- 计算机网络 --万维网www
万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...
- 九度OJ 1013:开门人和关门人 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5052 解决:2563 题目描述: 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请 ...
- 洛谷 4568 [JLOI2011] 飞行路线
题目戳这里 一句话题意: 有n个点,m条边的有向图,最多可以把k条边变为0,求从起点到终点最短距离. Solution 首先看到这题目,感觉贼难,看起来像DP,貌似也有大佬这么做,但鉴于本蒟蒻思维能力 ...
- Django框架ORM单表添加表记录_模型层
此方法依赖的表时之前创建的过的一张表 参考链接:https://www.cnblogs.com/apollo1616/p/9840354.html 方法1: # 语法 [变量] = [表名].obje ...
- lk进kernel
-- ] [upmu_is_chr_det] [] DRAM Rank : [] DRAM Rank[] Start = 0x40000000, Size = 0x25fc0000 [] DRAM R ...
- CodeForces - 540B School Marks —— 贪心
题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...
- 计算机中丢失OPENGL.dll
开发OpenGL项目时,在VS开发环境下可能会出现如图所示的错误. 在c:\windows\system32和SysWow64文件夹下存在opengl32.dll,此时,所写程序能够正常编译,但是,程 ...
- smokeping高级配置
摘自: http://mayulin.blog.51cto.com/1628315/514367 自定义报警 http://www.cnblogs.com/thatsit/p/6395506.html
- 使用C++模拟C#的委托机制
1. [代码][C/C++]代码 //Event.h #ifndef _EVENT_H_#define _EVENT_H_class EmptyObject {};template<typen ...