HDU 2732 Leapin' Lizards(拆点+最大流)

传送门:点击打开链接

参考博客:点击打开链接

Leapin' Lizards

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


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.

有N个柱子..有些柱子上有蜥蜴,有的没有..每只蜥蜴一次最多跳k距离..一个柱子只能跳有限次..问最后有几只蜥蜴跳不出来。


拆点建图的网络流,拆点后距离为1,建立超级源点0,超级汇点2*n*m+1,连接超级汇点的边流量为无限大,有蜥蜴的点到源点的流量为1,最后跑一遍最大流,记录蜥蜴总数,总数减去最大流的返回值就是答案。更详细的题解请去看参考博客,我只是对代码进行了解释。


代码实现:


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct node{
	int v,w,next;
}edge[maxn*maxn];

int head[maxn],d[maxn],start,END,cnt,sum,n;
char map[25][25],value[25][25];

void add(int u,int v,int w)            
{
	edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v= u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

int bfs()
{
	int i,j,k;
	mset(d,0);
	d[start]=1;
	queue <int> q;
	q.push(start);
	while(!q.empty())
	{
		int temp=q.front();
		q.pop();
		if(temp==END)                   //到达汇点 
		return 1;
		
		for(i=head[temp];i!=-1;i=edge[i].next) //搜增广路 
		{
			int temp1=edge[i].v;               //下一可连通点 
			int temp2=edge[i].w;               //当前流量 
			if(temp2&&d[temp1]==0)             //temp1没走到 
			{
				d[temp1]=d[temp]+1;            //点加1 
				if(temp1==END)                 //找到增广路 
				return 1;
				q.push(temp1);                 //入队列 
			}
		}
	}
	return 0;
}

int dfs(int u,int maxflow)
{
	if(u==END)
	return maxflow;
	
	int i,j,ret=0;
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int temp1=edge[i].v;            //下一个可连通点 
		int temp2=edge[i].w;            //当前点的流量 
		if(temp2&&d[temp1]==d[u]+1)
		{
			int temp=dfs(temp1,min(maxflow-ret,temp2)); //下一点,取最大流跟当前流量小的一个 
			edge[i].w-=temp;                  //正向相减 
			edge[i^1].w+=temp;                //反向弧相加 
			ret+=temp;                        //最大流 
			if(ret==maxflow)                  //下一点最大流等于这一点 
			return ret;
		}
	}
	return ret;
}

int dinic()
{
	int ans=0;
	while(bfs()==1)
	{
		ans+=dfs(start,inf);      //dfs返回每次能出去的蜥蜴的数量 
	}
	return ans;
}

int main()
{
	int t,flag=1,i,j,k,step;
	cin>>t;
	while(t--)
	{
		cnt=sum=0;
		mset(head,-1);
		scanf("%d%d",&n,&step); 
		getchar();
		for(i=1;i<=n;i++)              //储存高度 
		scanf("%s",value[i]);
		for(i=1;i<=n;i++)              //储存地图 
		scanf("%s",map[i]);
		int len=strlen(value[1]);      //得到地图的长度 
		start=0;END=n*len*2+1;         //源点是0,i+len*n表示蜥蜴出去,所以汇点为 len*n+len*n 
		for(i=1;i<=n;i++)              //枚举地图跟高度 
		{
			for(j=0;j<len;j++)
			{
				int in=(i-1)*len+j+1;              //当前点的位置 
				int out=(i-1)*len+j+1+n*len;       //对应的拆点的位置 
				if(map[i][j]=='L')                 //当前点有蜥蜴 
				{
					add(start,in,1);               //从起点到此点流量为1 
					sum++;                         //记录总的蜥蜴数 
				}
				if(value[i][j]=='0')               //当前点不能走 
				continue;
				add(in,out,value[i][j]-'0');      //拆点后流量为高度 
				if(i+step>n||i-step<1||j+step>=len||j-step<0) //可以直接跳出网格 
                add(out,END,inf);                 //拆点到汇点流量无限大 
                else                              //不可以直接跳出 
                {
                    for(int x=1;x<=n;x++)         //枚举剩余点 
                    for(int y=0;y<len;y++)
                    {
                        if(value[x][y]=='0')     //不能走 
						continue;
                        if(x==i&&y==j)           //当前点 
						continue;
                        if(abs(x-i)+abs(y-j)<=step) //距离小于d的流量无限大 
                        add(out,(x-1)*len+y+1,inf);
                    }
                }
			}
		}
		int ans=dinic();                          //返回能出去的蜥蜴数 
		if(sum-ans==0)
    	printf("Case #%d: no lizard was left behind.\n", flag++);
    	else if(sum-ans==1)
    	printf("Case #%d: 1 lizard was left behind.\n", flag++);
    	else
    	printf("Case #%d: %d lizards were left behind.\n", flag++, sum-ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值