题目链接:https://vjudge.net/problem/HDU-2732
Leapin' Lizards
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3231 Accepted Submission(s): 1326
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.
Source
Recommend
zty
题意:
在一个n*m的地图上, 有一些高度不一柱子, 又有一些青蛙站在柱子上,且一根柱子最多只能站一只青蛙。青蛙一次最多可跳跃d个距离,即:abs(x-xx)+abs(y-yy)<=d,且每跳一次,青蛙原来站着的柱子的高度会下降一个单位(作用力与反作用力?),当柱子的高度为0时,就无效了。当青蛙跳出界时,才算安全,问:最少有多少个青蛙不能跳出地图?
题解:
可用网络流建模求解,建图方法如下:
1.将每个柱子拆成两个点u、u',u用于跳入,u'用于跳出,且连一条边:u-->u',容量为高度,以限制最大跳跃次数。
2.设置超级源点,如果一根柱子上有青蛙,那么从超级源点向该柱子连一条边,容量为1,表明有一只青蛙。
3.设置超级汇点,如果从某根柱子上能够一步跳出地图,那么就从该柱子往超级汇点连一条边,容量为该柱子的高度(容量),表明从这根柱子跳出界的青蛙最多能有多少只。
4.如果u柱子能跳到v柱子,那么就连一条边u-->v,容量为u柱子的高度(容量),表明从u柱子最多能有多少只青蛙跳到v柱子。
5.跑最大流算法,所求得的就是能跳出地图的最大青蛙数,再用总的青蛙数减之,就是答案。
领接矩阵:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int mod = 1e9+7; 17 const int MAXM = 1e5+10; 18 const int MAXN = 1e3+10; 19 20 int maze[MAXN][MAXN]; 21 int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN]; 22 int flow[MAXN][MAXN]; 23 24 int sap(int start, int end, int nodenum) 25 { 26 memset(cur, 0, sizeof(cur)); 27 memset(dis, 0, sizeof(dis)); 28 memset(gap, 0, sizeof(gap)); 29 memset(flow, 0, sizeof(flow)); 30 int u = pre[start] = start, maxflow = 0, aug = INF; 31 gap[0] = nodenum; 32 33 while(dis[start]<nodenum) 34 { 35 loop: 36 for(int v = cur[u]; v<nodenum; v++) 37 if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1) 38 { 39 aug = min(aug, maze[u][v]-flow[u][v]); 40 pre[v] = u; 41 u = cur[u] = v; 42 if(v==end) 43 { 44 maxflow += aug; 45 for(u = pre[u]; v!=start; v = u, u = pre[u]) 46 { 47 flow[u][v] += aug; 48 flow[v][u] -= aug; 49 } 50 aug = INF; 51 } 52 goto loop; 53 } 54 55 int mindis = nodenum-1; 56 for(int v = 0; v<nodenum; v++) 57 if(maze[u][v]-flow[u][v]>0 && mindis>dis[v]) 58 { 59 cur[u] = v; 60 mindis = dis[v]; 61 } 62 if((--gap[dis[u]])==0) break; 63 gap[dis[u]=mindis+1]++; 64 u = pre[u]; 65 } 66 return maxflow; 67 } 68 69 int T, n, m, d; 70 bool inbroad(int x, int y) 71 { 72 return (x>=0 && x<n && y>=0 && y<m); 73 } 74 75 char pillar[25][25], lizard[25][25]; 76 int id[25][25], pnum, lnum; 77 int main() 78 { 79 scanf("%d", &T); 80 for(int kase = 1; kase<=T; kase++) 81 { 82 scanf("%d%d", &n, &d); 83 for(int i = 0; i<n; i++) scanf("%s", pillar[i]); 84 for(int i = 0; i<n; i++) scanf("%s", lizard[i]); 85 m = strlen(pillar[0]); 86 87 pnum = 0; lnum = 0; 88 for(int i = 0; i<n; i++) 89 for(int j = 0; j<m; j++) 90 { 91 if(lizard[i][j]=='L') lnum++; 92 if(pillar[i][j]-'0') id[i][j] = pnum++; 93 } 94 95 int start = 2*pnum, end = 2*pnum+1, N = 2*pnum+2; 96 memset(maze, 0, sizeof(maze)); 97 for(int i = 0; i<n; i++) 98 for(int j = 0; j<m; j++) 99 { 100 int cap = pillar[i][j]-'0'; 101 if(cap) 102 { 103 if(lizard[i][j]=='L') maze[start][id[i][j]] = 1; 104 maze[id[i][j]][pnum+id[i][j]] = cap; 105 bool flag = false; 106 for(int xd = -d; xd<=d; xd++) //枚举横坐标方向 107 for(int yd = abs(xd)-d; yd<=d-abs(xd); yd++) //枚举纵坐标方向 108 { 109 if(inbroad(i+xd, j+yd) && (pillar[i+xd][j+yd]-'0')) maze[pnum+id[i][j]][id[i+xd][j+yd]] = cap; 110 if(!inbroad(i+xd, j+yd)) flag = true; 111 } 112 if(flag) maze[pnum+id[i][j]][end] = cap; 113 } 114 } 115 116 int left = lnum - sap(start, end, N); 117 if(left==0) printf("Case #%d: no lizard was left behind. ", kase); 118 else if(left==1) printf("Case #%d: 1 lizard was left behind. ", kase); 119 else printf("Case #%d: %d lizards were left behind. ", kase, left); 120 } 121 }
邻接表:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int mod = 1e9+7; 17 const int MAXM = 1e5+10; 18 const int MAXN = 1e3+10; 19 20 struct Edge 21 { 22 int to, next, cap, flow; 23 }edge[MAXM]; 24 int tot, head[MAXN]; 25 int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; 26 27 void init() 28 { 29 tot = 0; 30 memset(head, -1, sizeof(head)); 31 } 32 33 void add(int u, int v, int w) 34 { 35 edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0; 36 edge[tot].next = head[u]; head[u] = tot++; 37 edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0; 38 edge[tot].next = head[v]; head[v] = tot++; 39 } 40 41 int sap(int start, int end, int nodenum) 42 { 43 memset(dep, 0, sizeof(dep)); 44 memset(gap, 0, sizeof(gap)); 45 memcpy(cur, head, sizeof(head)); 46 int u = pre[start] = start, maxflow = 0,aug = INF; 47 gap[0] = nodenum; 48 while(dep[start]<nodenum) 49 { 50 loop: 51 for(int i = cur[u]; i!=-1; i = edge[i].next) 52 { 53 int v = edge[i].to; 54 if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+1) 55 { 56 aug = min(aug, edge[i].cap-edge[i].flow); 57 pre[v] = u; 58 cur[u] = i; 59 u = v; 60 if(v==end) 61 { 62 maxflow += aug; 63 for(u = pre[u]; v!=start; v = u,u = pre[u]) 64 { 65 edge[cur[u]].flow += aug; 66 edge[cur[u]^1].flow -= aug; 67 } 68 aug = INF; 69 } 70 goto loop; 71 } 72 } 73 int mindis = nodenum; 74 for(int i = head[u]; i!=-1; i = edge[i].next) 75 { 76 int v=edge[i].to; 77 if(edge[i].cap-edge[i].flow && mindis>dep[v]) 78 { 79 cur[u] = i; 80 mindis = dep[v]; 81 } 82 } 83 if((--gap[dep[u]])==0)break; 84 gap[dep[u]=mindis+1]++; 85 u = pre[u]; 86 } 87 return maxflow; 88 } 89 90 int T, n, m, d; 91 bool inbroad(int x, int y) 92 { 93 return (x>=0 && x<n && y>=0 && y<m); 94 } 95 96 char pillar[25][25], lizard[25][25]; 97 int id[25][25], pnum, lnum; 98 int main() 99 { 100 scanf("%d", &T); 101 for(int kase = 1; kase<=T; kase++) 102 { 103 scanf("%d%d", &n, &d); 104 for(int i = 0; i<n; i++) scanf("%s", pillar[i]); 105 for(int i = 0; i<n; i++) scanf("%s", lizard[i]); 106 m = strlen(pillar[0]); 107 108 pnum = 0; lnum = 0; 109 for(int i = 0; i<n; i++) 110 for(int j = 0; j<m; j++) 111 { 112 if(lizard[i][j]=='L') lnum++; 113 if(pillar[i][j]-'0') id[i][j] = pnum++; 114 } 115 116 int start = 2*pnum, end = 2*pnum+1, N = 2*pnum+2; 117 init(); 118 119 for(int i = 0; i<n; i++) 120 for(int j = 0; j<m; j++) 121 { 122 int cap = pillar[i][j]-'0'; 123 if(cap) 124 { 125 if(lizard[i][j]=='L') add(start, id[i][j], 1); 126 add(id[i][j], pnum+id[i][j], cap); 127 bool flag = false; 128 for(int xd = -d; xd<=d; xd++) //枚举横坐标方向 129 for(int yd = abs(xd)-d; yd<=d-abs(xd); yd++) //枚举纵坐标方向 130 { 131 if(inbroad(i+xd, j+yd) && (pillar[i+xd][j+yd]-'0')) add(pnum+id[i][j], id[i+xd][j+yd], cap); 132 if(!inbroad(i+xd, j+yd)) flag = true; 133 } 134 if(flag) add(pnum+id[i][j], end, cap); 135 } 136 } 137 138 int left = lnum - sap(start, end, N); 139 if(left==0) printf("Case #%d: no lizard was left behind. ", kase); 140 else if(left==1) printf("Case #%d: 1 lizard was left behind. ", kase); 141 else printf("Case #%d: %d lizards were left behind. ", kase, left); 142 } 143 }