链接:

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(最大流, 拆点)的更多相关文章

  1. hdu 2732 Leapin' Lizards 最大流 拆点 建图

    题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...

  2. hdu 2732 Leapin' Lizards (最大流 拆点建图)

    Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...

  3. POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

  4. HDU 2732 Leapin' Lizards

    网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...

  5. HDU - 2732 Leapin' Lizards (拆点最大流)

    题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...

  6. HDU 2732 Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...

  7. hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005

    废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...

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

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

  9. HDU 2732 Leapin&#39; Lizards(拆点+最大流)

    HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...

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

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

随机推荐

  1. ABAP开发常用函数

    1.  LAST_DAY_OF_MONTHS——用于获取某月的最后一天. 例如输日今天的日期2013/10/16,系统将回执成2013年10月的最后一天,也就是2013/10/30 2.  CONVE ...

  2. 【HANA系列】SAP HANA XS创建XSJOB后台执行

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS创建XSJ ...

  3. GIS学习之栅格数据

    栅格数据用一个规则格网来描述与每一个格网单元位置相对应的空间现象特征的位置和取值.在概念上,空间现象的变化由格网单元值的变化来反映.地理信息系统中许多数据都用栅格格式来表示.栅格数据在许多方面是矢量数 ...

  4. Visual Subst - 简单将任意文件夹挂载模拟成驱动器盘符硬盘分区的小工具

    随着电脑的使用,硬盘里的资料一天比一天多,也越来越杂乱.一些朋友为了方便文件管理,会考虑重新分区,让C.D.E等盘符分别担任不同的角色.不过,不分区的话也有一些小工具可以帮你实现. Visual Su ...

  5. Myeclipse下配置SVN报错问题 svn: E175002: java.lang.RuntimeException: Could not generate DH keypair(转)

    转:http://blog.csdn.net/yulong_1988/article/details/51459936 在myeclipse下安装svn插件,出现了Could not generate ...

  6. 文件的上传(1)(表单上传和ajax文件异步上传)

    文件的上传(表单上传和ajax文件异步上传) 项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举 ...

  7. 第十四周课程总结&&实验总结

    课程总结: 1.Java实现跨平台操作的工具:JDBC. 它的意思是java数据库连接,可以方便的实现多种关系型数据库的统一操作,它由一组用java语句编写的类和接口组成. 2.JDBC驱动分类: J ...

  8. CDH5.X文档

    属性参数 https://www.cloudera.com/documentation/enterprise/properties.html

  9. Tomcat使用时出现的问题总结

    1.有两种办法解决Tomcat启动时端口号冲突问题 1.第一种: 查看本地端口使用情况,找到被占用的8080端口,杀死该进程 1.查看本地端口命令:cmd->netstat -ano 2.找到 ...

  10. redis 哈希 数据类型

    哈希 hset  设置哈希表字段 hset 8000 ename tom   hset 8000 job salesman hget 8000 ename  "tom" hget  ...