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.
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.
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.
题意:给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d
如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x
即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.
建图:
源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.
如果格子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(列号之差)
最终我们求出的最大流就是能跳出网格的蜥蜴数.
网络流的拆点操作是为了解决有些题目要求每个点的选择次数是有限制的.
比如 id 这个点的被选择次数最大是3,那么我们把它拆成id1 跟 id2两个点分别来表示入id和出id
再在id1和id2之间建一条容量为3的边,这样每次进入id的时候我们就可以让它其实进入id1然后原地跳一下跳到id2这样就控制了某个点的选择的次数
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 1000; 5 const int inf = 0x3f3f3f3f; 6 char s[100][100]; 7 char ss[100][100]; 8 struct Edge 9 { 10 int from,to,cap,flow; 11 Edge (){} 12 Edge (int f,int t,int c,int fl){from=f,to=t,cap=c,flow=fl;} 13 }; 14 struct Dinic 15 { 16 int n,m,s,t; 17 vector <Edge> edges; 18 vector <int> G[maxn]; 19 int cur[maxn]; 20 int dep[maxn]; 21 bool vis[maxn]; 22 void init (int n,int s,int t) 23 { 24 this->n=n;this->s=s;this->t=t; 25 edges.clear(); 26 for (int i=0;i<n;++i) 27 G[i].clear(); 28 } 29 void addedge (int from,int to,int cap) 30 { 31 edges.push_back(Edge(from,to,cap,0)); 32 edges.push_back(Edge(to,from,0,0)); 33 m = edges.size(); 34 G[from].push_back(m-2); 35 G[to].push_back(m-1); 36 } 37 bool bfs () 38 { 39 queue <int> q; 40 while (!q.empty()) q.pop(); 41 memset(vis,false,sizeof vis); 42 vis[s] = true; 43 dep[s] = 0; 44 q.push(s); 45 while (!q.empty()){ 46 int u = q.front(); 47 q.pop(); 48 for (int i=0;i<G[u].size();++i){ 49 Edge e = edges[G[u][i]]; 50 int v = e.to; 51 if(!vis[v]&&e.cap>e.flow){ 52 vis[v] = true; 53 dep[v] = dep[u] + 1; 54 q.push(v); 55 } 56 } 57 } 58 return vis[t]; 59 } 60 int dfs (int x,int mi){ 61 if (x==t||mi==0) return mi; 62 int flow = 0,f; 63 for (int &i=cur[x];i<G[x].size();++i){ 64 Edge &e = edges[G[x][i]]; 65 int y = e.to; 66 if (dep[y]==dep[x]+1&&(f=dfs(y,min(mi,e.cap-e.flow)))>0){ 67 e.flow+=f; 68 edges[G[x][i]^1].flow-=f; 69 flow+=f; 70 mi-=f; 71 if (mi==0) break; 72 } 73 } 74 return flow; 75 } 76 int max_flow () 77 { 78 int ans = 0; 79 while (bfs()){ 80 memset(cur,0,sizeof cur); 81 ans+=dfs(s,inf); 82 } 83 return ans; 84 } 85 }dinic; 86 int full_flow; 87 bool check (int x,int y,int i,int j,int d) 88 { 89 if (abs(x-i)+abs(y-j)<=d) return true; 90 else return false; 91 } 92 bool out (int x,int y,int d) 93 { 94 if (x-d) return true; 95 else return false; 96 } 97 int main() 98 { 99 //freopen("de.txt","r",stdin); 100 int casee = 0; 101 int T; 102 scanf("%d",&T); 103 while (T--){ 104 int n,m,src,dst,d; 105 full_flow = 0; 106 scanf("%d%d",&n,&d); 107 for (int i=1;i<=n;++i) 108 scanf("%s",s[i]+1); 109 int len = strlen(s[1]+1); 110 m = len; 111 src = 0; dst = 2*n*m+1; 112 dinic.init(2*n*m+2,src,dst); 113 for (int i=1;i<=n;++i){ 114 for (int j=1;j<=len;++j){ 115 if (s[i][j]-'0'>0){ 116 int id = (i-1)*m+j; 117 dinic.addedge(id,id+n*m,s[i][j]-'0'); 118 if (i<=d || i+d>n || j<=d || j+d>m){//这个点能直接跳出去 119 dinic.addedge(id+n*m,dst,inf); 120 } 121 else{ 122 for (int x=1;x<=n;++x){ 123 for (int y=1;y<=m;++y){ 124 if (x==i&&y==j) continue; 125 if (check(x,y,i,j,d)){ 126 int id2 = (x-1)*m+y; 127 dinic.addedge(id+n*m,id2,inf);//这个点的出连向能到达点的入 128 //dinic.addedge(id2+n*m,id,inf); 129 } 130 } 131 } 132 } 133 } 134 135 } 136 } 137 for (int i=1;i<=n;++i){ 138 scanf("%s",ss[i]+1); 139 for (int j=1;j<=len;++j){ 140 if (ss[i][j]=='L'){ 141 full_flow++; 142 int id = (i-1)*m+j; 143 dinic.addedge(src,id,1); 144 } 145 } 146 } 147 int ans = full_flow-dinic.max_flow(); 148 if(ans==0) printf("Case #%d: no lizard was left behind. ",++casee); 149 else if(ans==1) printf("Case #%d: 1 lizard was left behind. ",++casee); 150 else printf("Case #%d: %d lizards were left behind. ",++casee,ans); 151 } 152 return 0; 153 }