您的位置:首页 > 其它

hdu 2732 Leapin' Lizards ( Dinic 最大流)【难】

2016-09-28 21:13 543 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2732


Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2316    Accepted Submission(s): 972

Problem Description

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 t
4000
he 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

The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by
a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the
pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position
where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is

always 1 ≤ d ≤ 3.

 

Output

For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.

 

Sample Input

4
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 #1: 2 lizards were left behind.
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.

 

Source

Mid-Central USA 2005

 

Recommend

zty   |   We have carefully selected several similar problems for you:  3338 1533 3416 1569 2883 

题目大意:给你两个图,第一个图中数字为柱子的高度,第二个图  ‘L’  为蜥蜴,蜥蜴每次从一个柱子最多能走d步,没走一次柱子高度会减一,问最后有几个蜥蜴被留在图中

解析:建个图,找最大流,源点为 0 ,汇点为 r = 2 * k + 1(k为图中元素的个数),建图步骤:

            1.  如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

            2.  如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

            3.  如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,inf)

            4.  如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,inf). 注意这里的距离是abs(行号之差)+abs(列号之差)

最终我们求出的最大流就是能跳出网格的蜥蜴数, 注意输出(巨坑,,,)

            借鉴大牛的博客:http://xwk.iteye.com/blog/2129229

代码如下:

#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;

struct node
{
int x, y, f;
bool L;
} p
;

typedef struct
{
int to, c, rec;
} Q;

vector<Q> mp
;
int pre
, vis
;

void add(int u, int v, int w)
{
Q e;
e.to = v; e.c = w; e.rec = mp[v].size();
mp[u].push_back(e);
e.to = u; e.c = 0; e.rec = mp[u].size() - 1;
mp[v].push_back(e);
}

int check(struct node a, struct node b, int d)
{
int dis = abs(a.x - b.x) + abs(a.y - b.y);
if(dis <= d)
return 1;
return 0;
}

int bfs(int l, int r)
{
queue<int> q;
memset(pre, 0, sizeof(pre));

q.push(l);
pre[l] = 1;
int i;
while(!q.empty())
{
int u = q.front(); q.pop();
for(i = 0; i < mp[u].size(); i++)
{
Q &e = mp[u][i];
if(!pre[e.to] && e.c > 0)
{
pre[e.to] = pre[u] + 1;
q.push(e.to);
}
}
}
return pre[r] > 0;
}

int dfs(int u, int r, int cur_flow)
{
if(u == r) return cur_flow;
for(int &i = vis[u]; i < mp[u].size(); i++) //用引用,下次再搜时从当前开始,节省时间
{
Q &e = mp[u][i];
if(e.c > 0 && pre[e.to] == pre[u] + 1)
{
int f = dfs(e.to, r, min(cur_flow, e.c));
if(f > 0)
{
e.c -= f;
mp[e.to][e.rec].c += f;
return f;
}
}
}
return 0;
}

int Dinic(int l, int r)
{
int ans = 0, f;
while(bfs(l, r))
{
memset(vis, 0, sizeof(vis));
while((f = dfs(l, r, inf)))
ans += f;
}
return ans;
}

int main()
{
int t, m, n, d, i, j, k, cnt = 0;
char s[110];
cin >> t;
while(t--)
{
k = 0;
scanf("%d%d", &m, &d);
for(i = 0; i < m; i++)
{
scanf("%s", s);
for(j = 0; s[j]; j++)
{
k++;
p[k].x = i;
p[k].y = j;
p[k].f = s[j] - '0';
}
}
n = strlen(s);
k = 0;
for(i = 0; i < m; i++)
{
scanf("%s", s);
for(j = 0; s[j]; j++)
{
k++;
if(p[k].f && s[j] == 'L') p[k].L = true;
else p[k].L = false;
}
}
int r = k * 2 + 1, num = 0;
for(i = 1; i <= k; i++)
{
if(p[i].f > 0)
{
add(i, i + k, p[i].f);
for(j = 1; j <= k; j++)
{
if(i == j ) continue;
if(p[j].f > 0 && check(p[i], p[j], d))
{
add(i + k, j, inf); //很重要,不能反向
}
}
if(p[i].x + d >= m || p[i].x - d < 0 || p[i].y + d >= n || p[i].y - d < 0)
add(i + k, r, p[i].f); // 能顺利出去的路径
if(p[i].L == true) { add(0, i, 1); num++; }
}

}

int ans = num - Dinic(0, r); //注意输出
if(ans == 1) printf("Case #%d: 1 lizard was left behind.\n", ++cnt);
else if(ans > 1) printf("Case #%d: %d lizards were left behind.\n", ++cnt, ans);
else printf("Case #%d: no lizard was left behind.\n", ++cnt);

for(i = 0; i < N; i++) mp[i].clear();
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: