Pleasant sheep and big wolf
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1601 accepted submission (s): 687
Problem descriptionin zjnu, there is a well-known prairie. and it attracts pleasant sheep and his companions to have a holiday. big big wolf and his families know about this, and quietly hid in the big lawn. as zjnu ACM/ICPC team, we have an obligation to protect pleasant sheep
And his companions to free from being disturbed by big wolf. we decided to build a number of unit fence whose length is 1. any wolf and sheep can not cross the fence. of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his companions to free from being disturbed by big wolf and his companions.
Inputthere are invalid cases.
For every case:
N and M (n, m <= 200)
Then N * M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big wolf and his companions.
Outputfor every case:
First line output "case P:", P is the p-th case;
The second line is the answer.
Sample Input
4 61 0 0 1 0 00 1 0 0 02 0 0 0 0 00 2 0 1 1 0
Sample output
Case 1: 4
Source2009 multi-university training contest 14
-Host by zjnu
Recommendgaojie
# Include <iostream> # include <cstdio> # include <cstring> # define INF 99999999 using namespace STD; int map [205] [205]; struct node {int U, V, f ;}; node E [1000000]; int first [50000], next [1000000], CC; int d [50000], gap [50000], curedge [50000], pre [50000]; int XX [4] = {, 0,-1}; int YY [4] = {,-}; inline void add_edge (int u, int V, int f) {e [CC]. U = u; E [CC]. V = V; E [CC]. F = f; next [CC] = first [u]; first [u] = cc; CC ++; E [CC]. U = V; E [CC]. V = u; E [CC]. f = 0; next [CC] = first [v]; first [v] = cc; CC ++;}; int sap_max_flow (int s, int T, int N) {int cur_flow = 0, flow_ans = 0, I, U, neck, TMP; memset (D, 0, sizeof (d); memset (GAP, 0, sizeof (GAP); memset (PRE,-1, sizeof (pre); for (I = 0; I <= N; I ++) curedge [I] = first [I]; Gap [0] = n + 1; u = s; while (d [s] <n + 1) {If (u = T) {cur_flow = inf; for (I = s; I! = T; I = E [curedge [I]. v) {If (cur_flow> E [curedge [I]. f) cur_flow = E [curedge [I]. f, neck = I ;}for (I = s; I! = T; I = E [curedge [I]. v) {TMP = curedge [I]; E [TMP]. f-= cur_flow; E [TMP ^ 1]. F + = cur_flow;} flow_ans + = cur_flow; u = neck;} for (I = curedge [u]; I! =-1; I = next [I]) if (E [I]. F & D [u] = d [E [I]. v] + 1) break; if (I! =-1) {curedge [u] = I; Pre [E [I]. v] = u; u = E [I]. v;} else {If (0 = -- gap [d [u]) break; curedge [u] = first [u]; for (TMP = N + 5, I = first [u]; I! =-1; I = next [I]) if (E [I]. f) TMP = min (TMP, d [E [I]. v]); D [u] = TMP + 1; ++ gap [d [u]; If (u! = S) u = pre [u];} return flow_ans;} int main () {int n, m; int TT = 0; while (scanf ("% d", & N, & M )! = EOF) {int I, j; memset (MAP, 0, sizeof (MAP); memset (E, 0, sizeof (e); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) scanf ("% d", & map [I] [J]); cc = 0; int S = N * m; int t = n * m + 1; memset (first,-1, sizeof (first); memset (next,-1, sizeof (next); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {int K; for (k = 0; k <4; k ++) {int Tx = I + XX [k]; int ty = J + YY [k]; if (TX <0 | TX> = n | ty <0 | ty> = m) continue; add_edge (I * m + J, TX * m + ty, 1);} If (Map [I] [J] = 1) add_edge (I * m + J, T, INF ); if (Map [I] [J] = 2) add_edge (S, I * m + J, INF) ;}} printf ("case % d: \ n ", ++ TT); printf ("% d \ n", sap_max_flow (S, T, T) ;}return 0 ;}