HDU-2732-leapin'Lizards(最大流, 拆点)
链接:
https://vjudge.net/problem/HDU-2732
题意:
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.
思路:
依然是拆点建图.每个柱子拆成入口和出口,权值为可用次数,出口连到别的点和边缘都为INF.
对每个有人的柱子,源点连一个权值为1的边.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 20+10;
const int INF = 1e9;
struct Edge
{
int from, to, cap;
};
vector<Edge> edges;
vector<int> G[MAXN*MAXN*4];
int Dis[MAXN*MAXN*4];
char Map1[MAXN][MAXN], Map2[MAXN][MAXN];
int n, m, s, t, d;
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge{from, to, cap});
edges.push_back(Edge{to, from, 0});
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
}
bool Bfs()
{
memset(Dis, -1, sizeof(Dis));
queue<int> que;
que.push(s);
Dis[s] = 0;
while (!que.empty())
{
int u = que.front();
que.pop();
// cout << u << endl;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
Dis[e.to] = Dis[u]+1;
que.push(e.to);
}
}
}
return Dis[t] != -1;
}
int Dfs(int u, int flow)
{
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
{
int tmp = Dfs(e.to, min(flow, e.cap));
// cout << "flow:" << e.from << ' ' << e.to << ' ' << tmp << endl;
e.cap -= tmp;
flow -= tmp;
edges[G[u][i]^1].cap += tmp;
res += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
}
int MaxFlow()
{
int res = 0;
while (Bfs())
{
res += Dfs(s, INF);
}
return res;
}
int main()
{
// freopen("test.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int T, cnt = 0;
cin >> T;
while (T--)
{
cin >> n >> d;
for (int i = 1;i <= n;i++)
cin >> (Map1[i]+1);
for (int i = 1;i <= n;i++)
cin >> (Map2[i]+1);
m = strlen(Map1[1]+1);
s = 0, t = n*m*2+1;
for (int i = s;i <= t;i++)
G[i].clear();
edges.clear();
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
//(i-1)*m+j
if (Map1[i][j] == 0)
continue;
int node = (i-1)*m+j;
AddEdge(node*2-1, node*2, Map1[i][j]-'0');
if (i <= d || i > n-d || j <= d || j > m-d)
{
// cout << i << ' ' << j << endl;
AddEdge(node*2, t, INF);
}
for (int z = 1;z <= n;z++)
{
for (int k = 1;k <= m;k++)
{
if (abs(z-i)+abs(k-j) > d)
continue;
if (i == z && j == k)
continue;
// cout << i << ' ' << j << ' ' << ' ' << z << ' ' << k << endl;
int nodeto = (z-1)*m+k;
AddEdge(node*2, nodeto*2-1, INF);
// cout << Map1[i][j]-'0' << endl;
}
}
}
}
int res = 0;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
if (Map2[i][j] == '.')
continue;
res++;
int node = (i-1)*m+j;
AddEdge(s, node*2-1, 1);
}
}
res -= MaxFlow();
if (res == 0)
cout << "Case #" << ++cnt << ": no lizard was left behind." << endl;
else if (res == 1)
cout << "Case #" << ++cnt << ": " << res << " lizard was left behind." << endl;
else
cout << "Case #" << ++cnt << ": " << res << " lizards were left behind." << endl;
}
return 0;
}
/*
10
3 1
111
111
111
LLL
LLL
LLL
*/
HDU-2732-leapin'Lizards(最大流, 拆点)的更多相关文章
- hdu 2732 Leapin' Lizards 最大流 拆点 建图
题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...
- hdu 2732 Leapin' Lizards (最大流 拆点建图)
Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- HDU 2732 Leapin' Lizards
网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...
- HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...
- HDU 2732 Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...
- hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...
- hdu2732 Leapin' Lizards 最大流+拆点
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...
- HDU 2732 Leapin' Lizards(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- HDU2732 Leapin' Lizards —— 最大流、拆点
题目链接:https://vjudge.net/problem/HDU-2732 Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) M ...
随机推荐
- shell命令find删除修改后带尾巴的重复的文件
命令:find . -name "*~" -delete 说明:在linux中 点号(.)表示当前目录,连续的连个点号(..)表示父级目录 作用:在linux中,我经常会遇到这样的 ...
- 【BW系列】SAP 讲讲BW/4 HANA和BW on HANA的区别
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BW系列]SAP 讲讲BW/4 HANA和BW ...
- unity中的常遇到的问题
1.使用unity的MovieTexture播放视频在物体上,对象只能在电脑上 2.移动端播放全屏视频 Handheld.PlayFullScreenMovie(),视频文件必须放置在Streamin ...
- Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...
- 解决 Illegal DefaultValue null for parameter type integer 异常
该异常是由 swagger 引起的 swagger 版本 1.9.2 解决原因:重新导入 swagger-annotations 和 swagger-models 版本 为 1.5.21 pom.xm ...
- java集群技术(转)
序言 越来越多的关键应用运行在J2EE(Java 2, Enterprise Edition)中,这些诸如银行系统和账单处理系统需要高的可用性(High Availability, HA),同时像Go ...
- 2019.07.09 纪中_B
错失AK记 2019.07.09[NOIP提高组]模拟 B 组 明明今天的题都很水,可就是没蒟蒻. 写题的时候: T0一眼高精(结果没切)T1看到2啊8啊果断转二进制观察,发现都是左移几位然后空出的位 ...
- [MtOI2019]永夜的报应 题解
题面 题面说的乱糟糟的看起来似乎是个可持久化线性基: 然而~ 一个式子搞定一切: a^b<=a+b 那么把所有数异或起来便是答案: #include <bits/stdc++.h> ...
- 洛谷 P1169 棋盘制作 题解
题面 这道题可以分成两部分来处理: 第一部分: 设f[i][j]表示右下角以(i,j)结尾的最大正方形的边长. 显然f[i][j]=min(f[i][j-1],f[i-1][j-1],f[i-1][j ...
- 杜恩德的新博客,都来看看-duende99
啊啊啊啊 https://home.cnblogs.com/u/duende99/