Click Open Link
Question: In an N * M matrix grassland, sheep and wolves are distributed, and each grid can only have 0 or 1 animal. Now, we need to separate all the wolves from the sheep using a fence. How can we put it? The number of fences is the least. How can we find the number?
Analysis: Think of a wolf group as a collection and a group of sheep as a collection. Then, set the Source and Sink points, and assign the distance between the two points and the points of the existing animals to 1. The figure shows that the number of fences is obtained, and the divergence points from the existing animals are assigned to 1, place a fence in this direction. Then we can find that the result is the minimum cut, that is, the maximum flow. Note that the data size is large, 200*200. If the difference between the Set Source and Sink is large (S = 0, E = N * m), it is easy to TLE.
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; // maximum stream ISAP + BFS + stack optimization const int maxn = 100010; // Number of vertices const int maxm = 400010; // Number of edges const int INF = 0 xfffffff; struct edge {int to, next, Cap, flow ;} edge [maxm]; // note that maxmint tol; int head [maxn]; int gap [maxn], DEP [maxn], cur [maxn]; void Init () {Tol = 0; memset (Head,-1, sizeof (head);} void addedge (int u, int V, int W, int RW = 0) {edge [tol]. to = V; edge [tol]. CAP = W; edge [tol]. flow = 0; edge [tol]. next = head [u]; head [u] = tol ++; edge [tol]. to = u; edge [tol]. CAP = RW; edge [tol]. flow = 0; edge [tol]. next = head [v]; head [v] = tol ++;} int Q [maxn]; void BFS (INT start, int end) {memset (DEP,-1, sizeof (DEP); memset (GAP, 0, sizeof (GAP); Gap [0] = 1; int front = 0, rear = 0; dep [end] = 0; Q [re Ar ++] = end; while (front! = Rear) {int u = Q [Front ++]; for (INT I = head [u]; I! =-1; I = edge [I]. Next) {int v = edge [I]. To; If (DEP [v]! =-1) continue; Q [rear ++] = V; Dep [v] = Dep [u] + 1; Gap [Dep [v] ++ ;}}} int s [maxn]; int SAP (INT start, int end, int N) {BFS (START, end); memcpy (cur, Head, sizeof (head )); int Top = 0; int u = start; int ans = 0; while (DEP [start] <n) {If (u = END) {int min = inf; int inser; For (INT I = 0; I <top; ++ I) {If (min> edge [s [I]. cap-edge [s [I]. flow) {min = edge [s [I]. cap-edge [S [I]. flow; inser = I ;}}for (INT I = 0; I <top; ++ I) {edge [s [I]. flow + = min; edge [s [I] ^ 1]. flow-= min;} ans + = min; Top = inser; u = edge [s [Top] ^ 1]. to; continue;} bool flag = false; int V; For (INT I = cur [u]; I! =-1; I = edge [I]. next) {v = edge [I]. to; If (edge [I]. cap-edge [I]. flow & Dep [v] + 1 = Dep [u]) {flag = true; cur [u] = I; break ;}} if (FLAG) {s [top ++] = cur [u]; u = V; continue;} int min = N; For (INT I = head [u]; I! =-1; I = edge [I]. next) {If (edge [I]. cap-edge [I]. flow & Dep [edge [I]. to] <min) {min = Dep [edge [I]. to]; cur [u] = I ;}} gap [Dep [u] --; If (! Gap [Dep [u]) return ans; Dep [u] = min + 1; Gap [Dep [u] ++; If (u! = Start) u = edge [s [-- top] ^ 1]. to;} return ans;} int dir [] [2] = {-1, 0}, {1, 0}, {0,-1}, {0, 1 }}; int main () {int n, m, Case = 1; while (scanf ("% d", & N, & M )! = EOF) {int S = N * m, t = n * m + 1; int n = N * m + 2, temp; Init (); for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {scanf ("% d ", & temp); If (temp = 2) {addedge (S, M * I + J, INF);} If (temp = 1) {addedge (M * I + J, T, INF) ;}for (int K = 0; k <4; ++ K) {int x = I + dir [k] [0]; int y = J + dir [k] [1]; if (x> = 0 & x <n & Y> = 0 & Y <m) addedge (M * I + J, M * x + y, 1) ;}} printf ("case % d: \ n % d \ n", Case ++, SAP (s, t, n) ;}return 0 ;}