Leapin' Lizards 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.
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.
题目大概是一群蜥蜴在房间遇到了机关,被困在几根柱子上,蜥蜴能跳跃一定距离,但是每根柱子有耐久,每次有蜥蜴从上面跳离是耐久度减1,耐久为0时柱子倒塌,同一时刻每根柱子上只能站1只蜥蜴,跳到房间边上再多1格即为逃离,问最多能有几只蜥蜴逃离。
首先给出房间的宽度n,然后给出蜥蜴的跳跃长度d,接下来有两张n*m的矩阵(m未知),第一个矩阵均为数字,代表柱子的耐久度,0代表没有柱子。
第二个矩阵都是字符,“L”代表有一只蜥蜴在对应位置,输入保证每一只蜥蜴开始时都在一根柱子上。

首先设置源点为0,汇点为(蜥蜴数+柱子数*2+1)(因为柱子有耐久,要拆点)。
将源点与每一只蜥蜴建边,权值为1。
将蜥蜴与距离在跳跃长度内的柱子建边,权值为INF。
将柱子拆点,权值为柱子耐久。
将离房间边距离在跳跃长度-1距离内的柱子与汇点建边(因为到房间边多1格才算逃离),权值为INF。
然后跑最大流

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
int ma3[50][50],ma4[50][50];
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void init()
{
	memset(head,-1,sizeof(head));
	memset(ma3,0,sizeof(ma3));
	memset(ma4,0,sizeof(ma4));
	ne=0;
	maxflow=0;
}
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
int bfs(int s,int t)
{
	while(!q.empty()) q.pop();
	memset(depth,-1,sizeof(depth));
	for(int i=0;i<=T;i++) cur[i]=head[i];
	depth[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(depth[e[i].to]==-1&&e[i].cost)
			{
				depth[e[i].to]=depth[u]+1;
				q.push(e[i].to);
			}
		}
	}
	if(depth[t]==-1) return -1;
	else return 1;
}
int dfs(int s,int t,int limit)
{
	if(!limit||s==t) return limit;
	int f,flow=0;
	for(int i=cur[s];i!=-1;i=e[i].next)
	{
		cur[s]=i;
		if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
		{
			flow+=f;
			limit-=f;
			e[i].cost-=f;
			e[i^1].cost+=f;
			if(!limit) break;
		}
	}
	return flow;
}
void dinic(int s,int t)
{
	while(bfs(s,t)!=-1)
	{
		maxflow+=dfs(s,t,INF);
	}
}
struct point
{
	int x,y;
}p[2500];
int caldis(point a,point b)
{
	return abs(a.x-b.x)+abs(a.y-b.y);
}
int main()
{
	int tt,nnu=0;
	scanf("%d",&tt);
	while(tt--)
	{	
		char ma1[50][50],ma2[50][50];
		int num1=0,num2=0,len;
		scanf("%d %d",&n,&m);
		init();
		for(int i=0;i<n;i++)
		{
			scanf("%s",ma1[i]);
			len=strlen(ma1[i]);
			for(int j=0;j<strlen(ma1[i]);j++)
			{
				if(ma1[i][j]-'0'>0)
				{
					num2++;
					p[num2].x=i;
					p[num2].y=j;
					ma3[i][j]=ma1[i][j]-'0';
					ma4[i][j]=num2;
				}
			}
		}
		S=0;
		for(int i=0;i<n;i++)
		{
			scanf("%s",ma2[i]);
			for(int j=0;j<strlen(ma2[i]);j++)
			{
				if(ma2[i][j]=='L')
				{
					num1++;
					add(S,num2*2+num1,1);
					add(num1+num2*2,ma4[i][j],INF);
				}
			}
		}
		T=num1+num2*2+1;
		for(int i=1;i<=num2;i++)
		{
			add(i,i+num2,ma3[p[i].x][p[i].y]);
			if(p[i].x+1<=m||n-p[i].x<=m||p[i].y+1<=m||len-p[i].y<=m) add(i+num2,T,INF);
			for(int j=i+1;j<=num2;j++)
			{
				if(caldis(p[i],p[j])<=m)
				{
					add(i+num2,j,INF);
					add(j+num2,i,INF);
				}
			}
		}
		dinic(S,T);
		if(num1-maxflow==0) printf("Case #%d: no lizard was left behind.",++nnu);
		else if(num1-maxflow==1) printf("Case #%d: 1 lizard was left behind.",++nnu);
		else  printf("Case #%d: %d lizards were left behind.",++nnu,num1-maxflow);
		puts("");
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值