链接:

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. apache虚拟目录配置实例

    apache虚拟目录配置实例 一.首先,开启虚拟主机配置 在文件httpd.conf中找到: include conf/extra/httpd-vhosts.conf #开启 二.对httpd-vho ...

  2. docker rm & docker rmi & docker prune 的差异

    docker rm & docker rmi & docker prune 的差异 docker rm : 删除一个或多个 容器 docker rmi :  删除一个或多个 镜像 do ...

  3. Linux小技巧:du -sh * —— 查询文件目录大小

    du -ach * #这个能看到当前目录下的所有文件占用磁盘大小和总大小 du -sh #查看当前目录总大小 du -sh * #查看所有子目录大小 du -sh ./* #查看当前目录下所有文件/文 ...

  4. Hive开发中使用变量的两种方法

    在使用hive开发数据分析代码时,经常会遇到需要改变运行参数的情况,比如select语句中对日期字段值的设定,可能不同时间想要看不同日期的数据,这就需要能动态改变日期的值.如果开发量较大.参数多的话, ...

  5. [Wpf]在C#中添加 collectionViewSource

    Products = new ObservableCollection<Product>(products); ProductOptions = new ObservableCollect ...

  6. RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较

    原文:RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 这是网上的一篇教程写的很好,不知原作 ...

  7. MySQL索引优化 笔记

    少取字段,建立合理的索引 表优化: 1 定长与变长分离 如果都是定长 查询比较快 因为每一行的字节都是固定的 fixed 2 常用字段和不常用字段要分离 用户表 常用 放主表 个人介绍不常用 还比较长 ...

  8. oracle中的多表查询和子查询以及一些注意事项

    多表查询就是使用两张表及其以上的查询.首先需要知道几个名词,笛卡尔积,内连接,外连接,子查询. 1)笛卡尔积 所谓笛卡尔积其实就是两张表数据的条数相乘得到的最后结果,例如表1有10条数据,表2有4条数 ...

  9. react-native底部导航栏实现

    react-native-tab-navigator实现: bottom.js代码如下: import React, {Component} from 'react'; import {StyleSh ...

  10. 利用ARouter实现组件间通信,解决子模块调用主模块问题

    如果你还没使用过ARouter请你按照这篇下面博客尝试使用下然后再往下看组件通信的内容(不然的话可能会懵逼)Android Studio接入ARouter以及简单使用 如果你使用过ARouter请继续 ...