HDU2732(KB11-K 最大流)
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
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
Sample Input
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 #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.
Source
//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 最大流)的更多相关文章
- HDU2732 Leapin' Lizards —— 最大流、拆点
题目链接:https://vjudge.net/problem/HDU-2732 Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) M ...
- 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'代表 ...
- 洛谷P3358 最长k可重区间集问题(费用流)
传送门 因为一个zz错误调了一个早上……汇点写错了……spfa也写错了……好吧好像是两个…… 把数轴上的每一个点向它右边的点连一条边,容量为$k$,费用为$0$,然后把每一个区间的左端点向右端点连边, ...
- codevs1227
费用流,其实是求传输一个容量为k的流的最大费用.主要是建图.原点为0,和1连上一条容量为k,费用为0的边,中间每个点拆成两个1和2,连上一条边,容量为k,费用为c,再连一条容量为比k大,费用为0的边, ...
- 【POJ】【3680】Intervals
网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...
- Java8 使用
Java8 使用 链接:https://www.jianshu.com/p/936d97ba0362 链接:https://www.jianshu.com/p/41de7b5ac7b9 本文主要总结了 ...
- 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/ ...
- Optimized Flow Migration for NFV Elasticity Control
NFV弹性控制中的流迁移优化 ABSTRACT 基于动态创建和移除网络功能实例,NFV在网络功能控制上有很大的弹性.比如,网络功能和并,网络功能拆分,负载均衡等等. 那么为了实现弹性控制,就需要网络流 ...
- [ ZJOI 2010 ] 网络扩容
\(\\\) Description 给定一张有向图,每条边都有一个容量 \(C\) 和一个扩容费用 \(W\). 这里扩容费用是指将容量扩大 \(1\) 所需的费用.求: 在不扩容的情况下, \(1 ...
随机推荐
- C++ 执行Windows cmd命令
#include <windows.h> #include <iostream> #include <cstdio> using namespace std; vo ...
- Tools - 文本编辑器Notepad++
00 - NotePad++ 官网 01 - Notepad++修改主题 依次点击设置---语言格式设置---选择主题,在显示界面中修改相关设置(背景色.前景色.字体等). 02 - Notepad+ ...
- 创建python3虚拟环境指令和冻结所安装的包
mkvirtualenv file_name -p python3 去掉后面的 P 和python3 代表创建python2环境 其中p代表路径的意思 冻结所安装包命令 pip freeze > ...
- [Umbraco] 在umbraco中开发xlst的小窍门
当你在umbraco开发xslt时也可以调用C#里的方法,具体方法参考如下 点击第二个按钮 点击右侧的"Get Extensions" 系统自带了工具类,里面有很多常用也很实用的方 ...
- oracle 用户权限相关
--查看数据库下的所有用户: select username from dba_users; --查看当前连接数据库的用户角色 SELECT * FROM USER_ROLE_PRIVS; -- 创建 ...
- Oracle 扩展表空间大小的几种方式
环境:windows操作系统 增加表空间大小的四种方法Meathod1:给表空间增加数据文件 ALTER TABLESPACE app_data ADD DATAFILE 'D:\ORACLE\PRO ...
- configure: error: You need a C++ compiler for C++ support.[系统缺少c++环境]
一.错误configure: error: You need a C++ compiler for C++ support.二.安装c++ compiler情况1.当您的服务器能链接网络时候[联网安装 ...
- gvim配置相关
用 vundle 来管理 vim 插件(包含配置文件vimrc和gvimrc) gvim插件管理神器:vundle的安装与使用 Vim插件管理Vundle Linux 下VIM的配置 Vim配置系列( ...
- python for dblp.xml
由于最近处理数据时涉及到dblp.xml,刚开始下载时dblp.xml只有300多M,但解压之后就有1.9G,没有什么东西能够打开,所以必须要用工具来处理,在python中sax包能够一边解析一边处理 ...
- nginx配置文件 nginx.conf 说明
#user nobody; #开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log ...