HDU - 2732 Leapin' Lizards (最大流建模提升)

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.

题意:

给你两个图,一个用0,1,2,3表示,一个用 L 或 . 表示。其中用L表示的图中,有L的位置表示有蜥蜴,没有L的位置表示没有蜥蜴。用数字表示的图中,数字表示当前位置柱子的高度,每次一个蜥蜴可以从一个柱子跳到距离d以内的另外一个柱子,每跳跃一次(注意是跳走后柱子才降低),当前柱子的高度就减一,问最后会有多少只蜥蜴被困在里面。

题解:

主要是建模,把外面当成一个超级汇点t,把每个柱子拆成两个点啊v->v`,flow = 柱子的高度,如果当前柱子有蜥蜴就让超级源点s->v ,flow = 1。如果当前柱子可以跳到另一个柱子j上就v`->j,flow = INF。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 910;

struct Edge
{
	int flow,to,rev;
	Edge() {}
	Edge(int a,int b,int c):to(a),flow(b),rev(c) {}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,int flow)
{
	E[from].push_back(Edge(to,flow,E[to].size()));
	E[to].push_back(Edge(from,0,E[from].size()-1));
}

int deep[MAXN];
int iter[MAXN];

bool BFS(int from,int to)
{
	memset(deep,-1,sizeof deep);
	deep[from] = 0;
	queue<int> Q;
	Q.push(from);
	while(!Q.empty())
	{
		int t = Q.front();
		Q.pop();
		for(int i=0 ; i<E[t].size() ; ++i)
		{
			Edge& e = E[t][i];
			if(e.flow > 0 && deep[e.to] == -1)
			{
				deep[e.to] = deep[t] + 1;
				Q.push(e.to);
			}
		}
	}
	return deep[to] != -1;
}

int DFS(int from,int to,int flow)
{
	if(from == to || flow == 0)return flow;
	for(int& i=iter[from] ; i<E[from].size() ; ++i)
	{
		Edge& e = E[from][i];
		if(e.flow > 0 && deep[e.to] == deep[from] + 1)
		{
			int nowflow = DFS(e.to,to,min(flow,e.flow));
			if(nowflow > 0)
			{
				e.flow -= nowflow;
				E[e.to][e.rev].flow += nowflow;
				return nowflow;
			}
		}
	}
	return 0;
}

int Dinic(int from,int to)
{
	int sumflow = 0;
	while(BFS(from,to))
	{
		memset(iter,0,sizeof iter);
		int mid;
		while((mid = DFS(from,to,INF)) > 0)sumflow += mid;
	}
	return sumflow;
}

char map[25][25];
char mapt[25][25];
int book[25][25];

int main()
{

	int T,N,D,M;
	char s[25];
	scanf("%d",&T);
	for(int _=1 ; _<=T ; ++_)
	{
		scanf("%d %d",&N,&D);
		for(int i=1 ; i<=N ; ++i)scanf("%s",&map[i]);
		for(int i=1 ; i<=N ; ++i)scanf("%s",&mapt[i]);
		int num = 0,ti = 0;
		M = strlen(map[1]);
		for(int i=1 ; i<=N ; ++i)
		{
			for(int j=0 ; j<M ; ++j)
			{
				if(mapt[i][j] == 'L')++num;
				if(map[i][j] != '0')book[i][j] = ++ti;
			}
		}
		for(int i=1 ; i<=N ; ++i)
		{
			for(int j=0 ; j<M ; ++j)
			{
				if(map[i][j] == '0')continue;
				int time = book[i][j];
				if(mapt[i][j] == 'L')Add(0,time,1);
				int t = (int)(map[i][j]-'0');
				Add(time,time+400,t);
				if(i<=D || i+D>N || j+1<=D || j+1+D>M)Add(time+400,MAXN-1,INF);
				int x,y;
				for(int k=-D ; k<=D ; ++k)
				{
					x = i + k;
					if(x > N)break;
					if(x < 1)continue;
					for(int f=-D ; f<=D ; ++f)
					{
						y = j + f;
						if(y<0 || map[x][y] == '0' || (abs(k)+abs(f))>D || (k==0 && f==0))continue;
						if(y+1 > M)break;
						Add(time+400,book[x][y],INF);
					}
				}
			}
		}
		num -= Dinic(0,MAXN-1);
		if(num>=2)printf("Case #%d: %d lizards were left behind.\n",_,num);
		else if(num)printf("Case #%d: %d lizard was left behind.\n",_,num);
		else printf("Case #%d: no lizard was left behind.\n",_);
		for(int i=0 ; i<MAXN ; ++i)E[i].clear();
	}

	return 0;
}


如果某个柱子上可以直接跳到外面就让v`->t,flow = INF。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值