HDU 2732 Leapin' Lizards(最大流)

Leapin' Lizards

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


 

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.

思路:拆点,把点权转化为边权

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

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

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

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

最终我们求出的最大流就是能跳出网格的蜥蜴数

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4010;
const int MAXM = 40010;
const int INF = 0x3f3f3f3f;
struct Edge1
{
	int from,to,cap,flow;
};
struct Dinic
{
	int n,m,s,t;
	vector<Edge1> edges;
	vector<int> G[MAXN];
	bool vis[MAXN];
	int d[MAXN];
	int cur[MAXN];
	void init(int n) 
	{
		this -> n = n;
		for(int i = 0; i <= n + 1; i++){
			G[i].clear();
		}
		edges.clear();
	}
	void AddEdge(int from,int to,int cap) 
	{
		edges.push_back((Edge1){from,to,cap,0});	
		edges.push_back((Edge1){to,from,0,0});
		m = edges.size();
		G[from].push_back(m - 2);
		G[to].push_back(m - 1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = 1;
		while(!Q.empty()) {
			int x = Q.front();
			Q.pop();
			for(int i = 0; i < G[x].size(); i++) {
				Edge1& e = edges[G[x][i]];
				if(!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[x] + 1;
					Q.push(e.to);
				}
			}
		}
		return vis[t];
	}
	int DFS(int x,int a)
	{
		if(x == t || a == 0) return a;
		int flow = 0,f;
		for(int& i = cur[x]; i < G[x].size(); i++) {
			Edge1& e = edges[G[x][i]];
			if(d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[G[x][i] ^ 1].flow -= f;
				flow += f;
				a -= f;
				if(a == 0) break;
			}
		}
		return flow;
	}
	int Maxflow(int s,int t) {
		this -> s = s,this -> t = t;
		int flow = 0;
		while(BFS()) {
			memset(cur,0,sizeof(cur));
			flow += DFS(s,INF);
		}
		return flow;
	}
}din;
char a[1005];


int main(void)
{
	
	int t,n,m,d,S,T,id1,id2,dis,sum,ans;
	int kase = 0;
	scanf("%d",&t);
	while(t--) {
		kase++;
		scanf("%d%d",&n,&d);
		for(int i = 0; i < n; i++) {
			scanf("%s",a);
			if(i == 0) {
				m = strlen(a);
				S = 0,T = 2 * n * m + 1;
				din.init(2 * n * m + 2);
			}
			for(int j = 0; j < m; j++) {
				if(a[j] - '0' > 0) {
					id1 = i * m + (j + 1);
					din.AddEdge(id1,id1 + n * m,a[j] - '0');
					if(i < d || j < d || (i + d) >= n || (j + d) >= m) {
						din.AddEdge(id1 + n * m,T,INF);
					}
					else {
						for(int k = 0; k < n; k++) {
							for(int p = 0; p < m; p++) {
								id2 = k * m + p + 1;
								if(id1 == id2) continue;
								dis = abs((k - i)) + abs((p - j));
								if(dis <= d) din.AddEdge(id1 + n * m ,id2,INF);
							}
						}
					}
				}
			}
		}
		sum = 0;
		for(int i = 0; i < n; i++) {
			scanf("%s",a);
			for(int j = 0; j < m; j++) {
				if(a[j] == 'L') {
					id1 = i * m + j + 1;
					sum++;
					din.AddEdge(S,id1,1);
				}
			}
		}
		int ans = sum - din.Maxflow(S,T);
		if(ans == 0) printf("Case #%d: no lizard was left behind.\n",kase);
		else if(ans == 1) printf("Case #%d: 1 lizard was left behind.\n",kase);
		else printf("Case #%d: %d lizards were left behind.\n",kase,ans);
	}
	return 0;
}
/*
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..
........
........
*/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值