Leapin' Lizards (最大流+Dinic+拆点)

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

题意:

给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出格

思路:参考(点击打开链接

(1)源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

(2) 如果格子i上有蜥蜴,那么从s到i有边(s,i,1).
(3)如果格子i能承受x次跳出,那么有边(i,i+n*m,x)
(4)  如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)
(5) 如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)
跑一边最大流,最终我们求出的最大流就是能跳出网格的蜥蜴数.


代码:

#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 4000
#define PI acos(-1.0)
#define INF 1e9  
using namespace std;
typedef long long ll;

struct Edge{
	int from,to,cap,flow;
	Edge(){}
	Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
}; 

struct Dinic{
	int n,m,s,t;
	vector<Edge>edges;
	vector<int>G[maxn];
	bool vis[maxn];
	int d[maxn];
	int cur[maxn];
	
	void init(int n,int s,int t){
		this->s=s;
		this->t=t;
		this->n=n;
		for(int i=0;i<n;i++){
			G[i].clear();
		}
		edges.clear();
	}
	
	void AddEdge(int from,int to,int cap){
		edges.push_back((Edge){from,to,cap,0});
		edges.push_back((Edge){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++){
				Edge &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++){
			Edge& 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 flow=0;
		while(BFS()){
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}DC; 

int main()
{
	int t;
	scanf("%d",&t);
	int kase;
	kase=0;
	while(t--){
		kase++;
		int n,m,d,src,dst;
		int sum=0;//蜥蜴数目
		scanf("%d%d",&n,&d);
		for(int i=1;i<=n;i++){
			string s;
			cin>>s;
			if(i==1){ //第一次进行操作
				m=s.size();
				src=0,dst=2*n*m+1;
				DC.init(2*n*m+2,src,dst);
			}
			for(int j=0;j<s.size();j++) if(s[j]-'0'>0){
				int id=(i-1)*m+j+1;  //当前节点编号
				DC.AddEdge(id,id+n*m,s[j]-'0'); 
				if(i<=d||i+d>n||j<d||j+d>=m){//当前格子可直接跳出棋盘
					DC.AddEdge(id+n*m,dst,INF);
				}
				else{ //不能直接跳出去
					for(int k=1;k<=n;k++)
					for(int kk=0;kk<m;kk++){
						int id2=(k-1)*m+kk+1;
						if(id==id2) continue;
						if(abs(i-k)+abs(j-kk)<=d)
						DC.AddEdge(id+n*m,id2,INF) ;
					}
				}
			} 
			
		}
		
		for(int i=1;i<=n;i++){
			string s;
			cin>>s;
			for(int j=0;j<s.size();j++){
				int id=(i-1)*m+j+1;
				if(s[j]=='L'){
					++sum;
					DC.AddEdge(src,id,1);
				}
			}
		}
		int ans=sum-DC.Maxflow();
		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;
}


.地址: 点击打开链接


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值