Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3050    Accepted Submission(s): 1251

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

 
点上有权,显然要拆点。
源点与蜥蜴连边
蜥蜴向所在柱子入点连边,容量为1。
从柱子跳出边界,与汇点连边
从柱子跳到下一根柱子,本跳的出点与下跳的入点连边
 //2017-08-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].w = ;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; int T, n, m, d;
string G1[], G2[]; int getId(int x, int y, int op){
if(op == )
return x*m+y+;//柱子入点编号
else if(op == )
return n*m+x*m+y+;//柱子出点编号
else
return *n*m+x*m+y+;//蜥蜴编号
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputK.txt", "r", stdin);
cin>>T;
int kase = ;
while(T--){
cin>>n>>d;
for(int i = ; i < n; i++)
cin>>G1[i];
for(int i = ; i < n; i++)
cin>>G2[i];
m = G1[].length();
int s = , t = *n*m+;
dinic.init(s, t);
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(G1[i][j] != ''){
add_edge(getId(i, j, ), getId(i, j, ), G1[i][j]-'');
for(int dx = -d; dx <= d; dx++){
for(int dy = -d; dy <= d; dy++){
if(!dx && !dy)continue;
int nx = i + dx;
int ny = j + dy;
if(abs(dx)+abs(dy) > d)continue;
if(nx< || nx>=n || ny< || ny>=m){
add_edge(getId(i, j, ), t, INF);//跳出边界,与汇点连边
continue;
}
if(G1[nx][ny]!=''){
add_edge(getId(i, j, ), getId(nx, ny, ), INF);//跳到下一根柱子,本跳出点与下跳入点连边
}
}
}
}
}
}
int cnt = ;
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(G2[i][j] == 'L'){
cnt++;
add_edge(s, getId(i, j, ), INF);
add_edge(getId(i, j, ), getId(i, j, ), );//蜥蜴向所在柱子入点连边,容量为1,INF则WA
}
}
}
int ans = dinic.maxflow();
if(cnt-ans > )
cout<<"Case #"<<++kase<<": "<<cnt-ans<<" lizards were left behind."<<endl;
else if(cnt-ans == )
cout<<"Case #"<<++kase<<": "<<cnt-ans<<" lizard was left behind."<<endl;
else
cout<<"Case #"<<++kase<<": no lizard was left behind."<<endl; }
return ;
}

HDU2732(KB11-K 最大流)的更多相关文章

  1. HDU2732 Leapin' Lizards —— 最大流、拆点

    题目链接:https://vjudge.net/problem/HDU-2732 Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. hdu2732 Leapin' Lizards 最大流+拆点

    Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...

  3. HDU2732 Leapin' Lizards 最大流

    题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...

  4. 洛谷P3358 最长k可重区间集问题(费用流)

    传送门 因为一个zz错误调了一个早上……汇点写错了……spfa也写错了……好吧好像是两个…… 把数轴上的每一个点向它右边的点连一条边,容量为$k$,费用为$0$,然后把每一个区间的左端点向右端点连边, ...

  5. codevs1227

    费用流,其实是求传输一个容量为k的流的最大费用.主要是建图.原点为0,和1连上一条容量为k,费用为0的边,中间每个点拆成两个1和2,连上一条边,容量为k,费用为c,再连一条容量为比k大,费用为0的边, ...

  6. 【POJ】【3680】Intervals

    网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...

  7. Java8 使用

    Java8 使用 链接:https://www.jianshu.com/p/936d97ba0362 链接:https://www.jianshu.com/p/41de7b5ac7b9 本文主要总结了 ...

  8. 2019.04.11 第四次训练 【 2017 United Kingdom and Ireland Programming Contest】

    题目链接:  https://codeforces.com/gym/101606 A: ✅ B: C: ✅ D: ✅ https://blog.csdn.net/Cassie_zkq/article/ ...

  9. Optimized Flow Migration for NFV Elasticity Control

    NFV弹性控制中的流迁移优化 ABSTRACT 基于动态创建和移除网络功能实例,NFV在网络功能控制上有很大的弹性.比如,网络功能和并,网络功能拆分,负载均衡等等. 那么为了实现弹性控制,就需要网络流 ...

  10. [ ZJOI 2010 ] 网络扩容

    \(\\\) Description 给定一张有向图,每条边都有一个容量 \(C\) 和一个扩容费用 \(W\). 这里扩容费用是指将容量扩大 \(1\) 所需的费用.求: 在不扩容的情况下, \(1 ...

随机推荐

  1. .NET Core 从1.1升级到2.0记录(Cookie中间件踩坑)

    .NET Core 2.0 新时代 万众瞩目的.NET Core 2.0终于发布了,原定于9.19的dotnetconf大会的发布时间大大提前了1个月,.NET Core 2.0/.NET Stand ...

  2. [原创]K8_C段旁注工具6.0 新增SMB漏洞扫描

    工具: K8_C段旁注工具6.0_0510[K.8]编译: 自己查壳组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.163.com发布: 201 ...

  3. Storm官方提供的trident单词计数的例子

    上代码: public class TridentWordCount { public static class Split extends BaseFunction { @Override publ ...

  4. [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associated with an element type "id".

    用DOM解析XML时出现了如下错误: [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associa ...

  5. C# 获取所有对象的字符串表示一ToString方法

    应用程序开发过程中经常需要获取对象的字符串表示.Object类中定义了一个ToString的虚方法.所以在任何类型的实例上都能调用该方法. C#中几乎所有的类型都派生自Object,所以如果当前类型没 ...

  6. SSH远程连接Ubuntu Server

    Ubuntu默认没有安装openssh-server包,故从远程计算机通过SSH连接时会收到Connection refused的消息. 首先在Ubuntu检查/etc/ssh/sshd_config ...

  7. 用SpringSecurity从零搭建pc项目-01

    注:之前写过一些列的SpringSecurity的文章,重新写一遍是为了把某些不必要的步骤省去,留下精简的,因为工作中有一些不需要. 在java的权限框架里,shiro和SpringSecurity是 ...

  8. 安装flutter和dart总结

    1 manjaro从软件仓库安装就行,但是也可以下载安装包.然后添加到Path目录 我是安装dart从软件仓库, flutter下载压缩包添加到path 1.1 需要将android sdk , ex ...

  9. es-02-elasticsearch安装及遇到的问题

    最近因为工作需要, 又使用到了es, 版本已经从当年的2.4 更新到了6.3 基本上解压即用, elasticsearch 5.x 版本, 在 centos6下, 很多性能不能够发挥, 建议 cent ...

  10. windows7(64位) PHP APACHE MYSQL

    - 一.安装软件准备软件版本以本人安装为例,其他版本同理,软件到各官网下载      1.Apache(httpd-2.2.19-win64)      2.PHP(php-5.3.6-Win32-V ...