HDU 2732 Leapin' Lizards(Dinic算法)

Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3664    Accepted Submission(s): 1498


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 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.
 

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

【思路】

題目給了一個“棋盤”,每個棋盤格子裏可能有一隻蜥蜴,棋盤格子允許被離開若干次,而蜥蜴只能跳到距離(距離定義:dist = 橫座標差的絕對值 + 縱座標差的絕對值)不大於d的格子上,問最後會有幾隻蜥蜴被剩下。

這可以轉化爲一個最大流問題,即求從源點s(棋盤)到終點t(外面)能流過多少隻蜥蜴。

做法:先定義一個超級源點s。由於每個點只允許被經過若干次,我們拆點,每個點的點1到點2之間建立一條容量爲它使用次數的邊。若棋盤格子上i有蜥蜴,則在s和i的點1建立一條容量爲1的邊。若格子i能直接跳出棋盤,則i的點2和t建立一條容量爲無窮大的邊。若點i無法跳出棋盤,纔在它的點2和它能到達的其它點的點1之間建立一條容量爲無窮大的邊。最後用Dinic算法跑一遍最大流,把答案處理出來。

【代碼】

//******************************************************************************
// File Name: HDU_2732.cpp
// Author: Shili_Xu
// E-Mail: [email protected]
// Created Time: 2018年07月12日 星期四 20時31分19秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const int MAXN = 1005, INF = 0x3f3f3f3f;

struct edge {
	int from, to, cap, flow;

	edge() {}
	edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};

int t, n, rows, len, leap;
vector<edge> e;
vector<int> g[MAXN];
queue<int> q;
int d[MAXN], cur[MAXN];
int step[25][25];
bool lizard[25][25], reach[25][25];

void init()
{
	e.clear();
	for (int i = 0; i <= n; i++) g[i].clear();
}

void add_edge(int u, int v, int c, int f)
{
	e.push_back(edge(u, v, c, f));
	g[u].push_back(e.size() - 1);
}

bool bfs(int s, int t)
{
	while (!q.empty()) q.pop();
	memset(d, 0, sizeof(d));
	d[s] = 1; q.push(s);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = 0; i < g[u].size(); i++) {
			edge &now = e[g[u][i]];
			if (d[now.to] || now.cap - now.flow == 0) continue;
			d[now.to] = d[u] + 1;
			q.push(now.to);
		}
	}
	return (d[t] != 0);
}

int dfs(int u, int t, int f)
{
	if (u == t || f == 0) return f;
	int ans = 0, add;
	for (int &i = cur[u]; i < g[u].size(); i++) {
		edge &now = e[g[u][i]];
		if (d[now.to] == d[u] + 1 && (add = dfs(now.to, t, min(f, now.cap - now.flow))) > 0) {
			e[g[u][i]].flow += add;
			e[g[u][i] ^ 1].flow -= add;
			ans += add;
			f -= add;
			if (f == 0) break;
		}
	}
	return ans;
}

int dinic(int s, int t)
{
	int ans = 0;
	while (bfs(s, t)) {
		memset(cur, 0, sizeof(cur));
		ans += dfs(s, t, INF);
	}
	return ans;
}

bool ok(int x, int y)
{
	return (1 <= x && x <= rows && 1 <= y && y <= len);
}

bool get(int i, int j, int k, int l)
{
	return (abs(i - k) + abs(j - l) <= leap);
}

int main()
{
	scanf("%d", &t);
	for (int kase = 1; kase <= t; kase++) {
		printf("Case #%d: ", kase);
		scanf("%d %d", &rows, &leap);
		char str[25];
		int number = 0;
		for (int i = 1; i <= rows; i++) {
			scanf("%s", str);
			len = strlen(str);
			for (int j = 1; j <= len; j++) step[i][j] = str[j - 1] - '0';
		}
		for (int i = 1; i <= rows; i++) {
			scanf("%s", str);
			for (int j = 1; j <= len; j++)
				if (str[j - 1] == '.')
					lizard[i][j] = false;
				else
					lizard[i][j] = true, number++;
		}
		n = len * rows * 2 + 1;
		init();
		for (int i = 1; i <= rows; i++)
			for (int j = 1; j <= len; j++)
				reach[i][j] = (get(i, j, 0, j) || get(i, j, rows + 1, j) || get(i, j, i, 0) || get(i, j, i, len + 1));
		for (int i = 1; i <= rows; i++) {
			for (int j = 1; j <= len; j++) {
				if (step[i][j] != 0) {
					add_edge((i - 1) * len + j, rows * len + (i - 1) * len + j, step[i][j], 0);
					add_edge(rows * len + (i - 1) * len + j, (i - 1) * len + j, 0, 0);
				}
				if (lizard[i][j]) {
					add_edge(0, (i - 1) * len + j, 1, 0);
					add_edge((i - 1) * len + j, 0, 0, 0);
				}
				if (reach[i][j] && step[i][j] != 0) {
					add_edge(rows * len + (i - 1) * len + j, n, INF, 0);
					add_edge(n, rows * len + (i - 1) * len + j, 0, 0);
				}
				else {
					for (int k = i - leap; k <= i + leap; k++)
						for (int l = j - leap; l <= j + leap; l++)
							if (ok(k, l) && (k != i || l != j) && step[i][j] && step[k][l] && get(i, j, k, l)) {
								add_edge(rows * len + (i - 1) * len + j, (k - 1) * len + l, INF, 0);
								add_edge((k - 1) * len + l, rows * len + (i - 1) * len + j, 0, 0);
							}
				}
			}
		}
		int ans = number - dinic(0, n);	
		if (ans == 0) printf("no lizard was left behind.\n");
		if (ans == 1) printf("1 lizard was left behind.\n");
		if (ans >= 2) printf("%d lizards were left behind.\n", ans);
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章