Leapin' Lizards [HDU - 2732]【网络流最大流】
网络流直接最大流就是了,只是要拆点小心一个点的流超出了原本的正常范围才是。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = , maxE = 3e4 + , st = ;
int N, D, M, all, head[], cur[], cnt, ed;
char liz[maxN][maxN], jump[maxN][maxN];
struct Eddge
{
int nex, to, flow;
Eddge(int a=-, int b=, int c=):nex(a), to(b), flow(c) {}
}edge[maxE];
inline void addEddge(int u, int v, int w)
{
edge[cnt] = Eddge(head[u], v, w);
head[u] = cnt++;
}
inline void _add(int u, int v, int w) { addEddge(u, v, w); addEddge(v, u, ); }
inline int _id(int x, int y) { return (x - ) * M + y; }
inline bool can_out(int x, int y) { return x <= D || y <= D || (N - x < D) || (M - y < D); }
bool In_map(int x, int y) { return x >= && y >= && x <= N && y<= M; }
int deep[];
queue<int> Q;
inline bool bfs()
{
for(int i=; i<=ed; i++) deep[i] = ;
while(!Q.empty()) Q.pop();
Q.push(st); deep[st] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u], v, f; ~i; i=edge[i].nex)
{
v = edge[i].to; f = edge[i].flow;
if(f && !deep[v])
{
deep[v] = deep[u] + ;
Q.push(v);
}
}
}
return deep[ed];
}
inline int dfs(int u, int dist)
{
if(u == ed) return dist;
for(int &i=cur[u], v, f; ~i; i=edge[i].nex)
{
v = edge[i].to; f = edge[i].flow;
if(f && deep[v] == deep[u] + )
{
int di = dfs(v, min(dist, f));
if(di)
{
edge[i].flow -= di;
edge[i^].flow += di;
return di;
}
}
}
return ;
}
inline int Dinic()
{
int ans = , tmp;
while(bfs())
{
for(int i=; i<=ed; i++) cur[i] = head[i];
while((tmp = dfs(st, INF))) ans += tmp;
}
return ans;
}
inline void init()
{
all =cnt = ;
memset(head, -, sizeof(head));
}
int main()
{
int T; scanf("%d", &T);
for(int Cas=; Cas<=T; Cas++)
{
scanf("%d%d", &N, &D);
init();
for(int i=; i<=N; i++) scanf("%s", jump[i] + );
M = (int)strlen(jump[] + );
ed = * N * M + ;
for(int i=; i<=N; i++)
{
scanf("%s", liz[i] + );
for(int j=; j<=M; j++)
{
if(liz[i][j] == 'L')
{
all++;
_add(st, _id(i, j), );
}
}
}
for(int i=, u, v; i<=N; i++)
{
for(int j=; j<=M; j++)
{
u = _id(i, j);
if(jump[i][j] > '')
{
_add(u, u + N * M, jump[i][j] - '');
if(can_out(i, j))
{
_add(u + N * M, ed, jump[i][j] - '');
continue;
}
for(int x=; x<=D; x++)
{
for(int y=D-x; y>=; y--)
{
if(!x && !y) continue;
if(In_map(i + x, j + y))
{
v = _id(i + x, j + y);
_add(u + N * M, v, jump[i][j] - '');
}
if(In_map(i - x, j + y))
{
v = _id(i - x, j + y);
_add(u + N * M, v, jump[i][j] - '');
}
if(In_map(i + x, j - y))
{
v = _id(i + x, j - y);
_add(u + N * M, v, jump[i][j] - '');
}
if(In_map(i - x, j - y))
{
v = _id(i - x, j - y);
_add(u + N * M, v, jump[i][j] - '');
}
}
}
}
}
}
printf("Case #%d: ", Cas);
int ans = all - Dinic();
if(ans == ) printf("no lizard was left behind.\n");
else if(ans == ) printf("1 lizard was left behind.\n");
else printf("%d lizards were left behind.\n", ans);
}
return ;
}
Leapin' Lizards [HDU - 2732]【网络流最大流】的更多相关文章
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- K - Leapin' Lizards HDU - 2732 网络流
题目链接:https://vjudge.net/contest/299467#problem/K 这个题目从数据范围来看可以发现是网络流,怎么建图呢?这个其实不是特别难,主要是读题难. 这个建图就是把 ...
- K - Leapin' Lizards - HDU 2732(最大流)
题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有 ...
- 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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Leapin' Lizards HDU - 2732 (恶心的建图。。)
这道题其实不难...就是建图恶心了点....emm... 题意: 多源多汇 + 拆边 青蛙跳柱子, 每根柱子都有一定的承载能力, 青蛙跳上去之后柱子的承载能力就会减一,跳到边界就能活 跳不到就over ...
- hdu 3549 网络流最大流 Ford-Fulkerson
Ford-Fulkerson方法依赖于三种重要思想,这三个思想就是:残留网络,增广路径和割. Ford-Fulkerson方法是一种迭代的方法.开始时,对所有的u,v∈V有f(u,v)=0,即初始状态 ...
- HDU 2732 Leapin' Lizards(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- Leapin' Lizards(经典建图,最大流)
Leapin' Lizards http://acm.hdu.edu.cn/showproblem.php?pid=2732 Time Limit: 2000/1000 MS (Java/Others ...
随机推荐
- 本地启oracle实例服务无法重启,协议适配器错误
今天遇到一位朋友的oracle实例服务无法起来,启动时报错: 分析的原因是可能早上服务器突然断电造成的,经过对tns的测试 经过我们讨论和诊断,最后诊断的处理方法是将实例删了重装,处理后服务恢复正常: ...
- Neo4j 修改关系类型 type
没有直接修改的函数,也不需要,下面代码就可以: MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar&qu ...
- SSM商城系统开发笔记-问题02- Error creating bean with name 'userController'
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wit ...
- 380-Xilinx Kintex UltraScale FPGA KCU1500 Acceleration Development Kit
Xilinx Kintex UltraScale FPGA KCU1500 Acceleration Development Kit Product Description The Kintex® U ...
- CentOS7安装mysql8.0编译报错集合
以下都是我安装mysql8.0遇到的一些报错和解决方法 1.does not appear to contain CMakeLists.txt. 原因:mysql下载的源码包不对 解决方法:下载正确的 ...
- java并发学习--第一章 线程的创建
所谓的并发就是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行.所以我们看似几个线程在同时进行,其实在操作系统中 ...
- 06.队列、python标准库中的双端队列、迷宫问题
class QueueUnderflow(ValueError): """队列为空""" pass class SQueue: def __ ...
- 00.斐波那契数列第n项
# 斐波那契数列第n项 # 1 1 2 3 5 8 def fib(n): if n <= 2: return 1 else: return fib(n-2)+fib(n-1) def fib2 ...
- 使用rabbitctl添加用户
使用rabbitctl添加用户 第一.添加mq用户并设置密码 root@live-mq-01:~ # rabbitmqctl add_user mq 123456 1 root@live-mq-01: ...
- 【leetcode】1028. Recover a Tree From Preorder Traversal
题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traver ...