Leapin' Lizards

http://acm.hdu.edu.cn/showproblem.php?pid=2732

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

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.

 
 
一道水题做一天。。。。真菜
拆点跑最大流就好,距离是曼哈顿距离
 
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define maxn 200005
#define MAXN 200005
#define mem(a,b) memset(a,b,sizeof(a))
const int N=;
const int M=;
const int INF=0x3f3f3f3f;
using namespace std;
int n;
struct Edge{
int v,next;
int cap,flow;
}edge[MAXN*];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;//实际存储总边数
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,int w)//加边
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,int w){
isap_add(u,v,w);
isap_add(v,u,);
}
bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty())
q.pop();
q.push(t);//从汇点开始反向建层次图
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
//if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
//break;
}
}
}
return dep[s]!=-;
}
int isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
//for(int i=1;i<=n;i++)
//cout<<"cur "<<cur[i]<<endl;
int u=s;
path[u]=-;
int ans=;
while(dep[s]<n)//迭代寻找增广路,n为节点数
{
if(u==t)
{
int f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])//修改找到的增广路
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;//当前弧优化
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;//gap优化
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;//常数优化
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)//当前点没有增广路则后退一个点
u=edge[path[u]^].v;
}
return ans;
} string mp[];
string book[];
int dir[][]={,,,,,-,-,};
int main(){
std::ios::sync_with_stdio(false);
int m,s,t;
int f,d;
int T;
cin>>T;
for(int co=;co<=T;co++){
cin>>n>>d;
for(int i=;i<n;i++) cin>>book[i];
for(int i=;i<n;i++) cin>>mp[i];
isap_init();
m=mp[].length();
int sum=;
s=n*m*,t=n*m*+;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]=='L'){
add(s,i*m+j,);
sum++;
}
}
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(book[i][j]!=''){
if(i<d||j<d||i+d>=n||j+d>=m){
add(m*n+i*m+j,t,book[i][j]-'');
}
add(i*m+j,n*m+i*m+j,book[i][j]-'');
for(int ii=-d;ii<=d;ii++){
for(int jj=-d;jj<=d;jj++){
int xx=i+ii,yy=j+jj;
if(xx>=&&xx<n&&yy>=&&yy<m){
if(book[xx][yy]!=''&&(ii||jj)){
if(abs(ii)+abs(jj)<=d){
add(n*m+i*m+j,xx*m+yy,INF);
}
}
}
}
}
}
}
}
n=n*m*+;
int ans=isap(s,t);
ans=sum-ans;
cout<<"Case #"<<co<<": ";
if(!ans) cout<<"no lizard was left behind."<<endl;
else if(ans==) cout<<"1 lizard was left behind."<<endl;
else cout<<ans<<" lizards were left behind."<<endl;
}
}

Leapin' Lizards(经典建图,最大流)的更多相关文章

  1. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  2. hdoj--5093--Battle ships(二分图经典建图)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. POJ1149 最大流经典建图PIG

    题意:       有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...

  4. BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图

    这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...

  5. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  6. BZOJ-1305 dance跳舞 建图+最大流+二分判定

    跟随YveH的脚步又做了道网络流...%%% 1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 2119 S ...

  7. zoj 3460 Missile【经典建图&&二分】

    Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Ev ...

  8. hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

    Problem Description Thanks to a certain "green" resources company, there is a new profitab ...

  9. 志愿者招募 HYSBZ - 1061(公式建图费用流)

    转自神犇:https://www.cnblogs.com/jianglangcaijin/p/3799759.html 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管. ...

随机推荐

  1. 杂项:Office Visio

    ylbtech-杂项:Office Visio Office Visio 是一款便于IT和商务人员就复杂信息.系统和流程进行可视化处理.分析和交流的软件.使用具有专业外观的 Office Visio ...

  2. 用linux的iconv函数 转换编码

    inux shell 配置文件中默认的字符集编码为UTF-8 .UTF-8是unicode的一种表达方式,gb2312是和unicode都是字符的编码方式,所以说gb2312跟utf-8的概念应该不是 ...

  3. MapReduce On YARN

    MapReduce计算框架 将计算过程分为两个阶段:Map和Reduce Map阶段并行处理输入数据: Reduce阶段对Map结果进行汇总 Shuffle连接Map和Reduce两个阶段 Map T ...

  4. while_else

    使用while循环输出100-50,从大到小,到50的时候,再从0到50输出,然后结束count =

  5. sorted()&enumerate()

    d = {1:2,3:1,44:5,4:5,7:8}l = d.items() #转换为列表print(l)  # dict_items([(1, 2), (3, 1), (44, 5), (4, 5 ...

  6. css模拟下拉菜单

    <!DOCTYPE html > <head>     <meta http-equiv="Content-Type" content="t ...

  7. JAVA JDBC 各大数据库的连接字符串和连接类

    oracle:     driverClass:oracle.jdbc.OracleDriver     url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mys ...

  8. redis详解(一)-- 概述

    首先,分布式缓存框架 可以 看成是nosql的一种 (1)什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的 ...

  9. 机器学习入门-数据过采样(上采样)1. SMOTE

    from imblearn.over_sampling import SMOTE  # 导入 overstamp = SMOTE(random_state=0) # 对训练集的数据进行上采样,测试集的 ...

  10. Ambari安装Hadoop集群

    * System Environment:centOS6.7 1.Prepare the Environment 1)Set Up Password-less SSH : (Generate publ ...