Obviously the smallest cut ... The wolf's territory even s, the sheep's territory even t, and then the middle of the edge, run the maximum flow is OK
--------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 10009;const int INF = 100000000; struct Edge {int to, cap;Edge *next, *rev;} e[100000], *pt = E, *HEAD[MAXN];inline void Add (int u, int v, int w) {pt->to = v; pt->cap = w; pt->next = Head[u]; Head[u] = pt++;}inline void Addedge (int u, int v, int w) {Add (U, V, W); Add (V, u, 0);Head[u]->rev = head[v];Head[v]->rev = Head[u];}Edge *P[MAXN], *CUR[MAXN];int CNT[MAXN], H[MAXN], mp[109][109], S, T, N; int Maxflow () {for (int i = 0; i < N; i++) cur[i] = Head[i];memset (CNT, 0, sizeof CNT); cnt[0] = N;memset (h, 0, sizeof h);int flow = 0;edge* E;for (int A = INF, x = S; H[s] < N;) {For (e = cur[x]; e; e = e->next)if (h[e->to] + 1 = = H[x] && e->cap) break;if (e) {P[e->to] = cur[x] = e;a = min (A, e->cap);x = e->to;if (x = = T) {flow + = A;For (; x! = S; x = p[x]->rev->to) {P[x]->cap-= A;P[x]->rev->cap + = A;}A = INF;}} else {if (!--Cnt[h[x]]) break;h[x] = N;For (e = head[x]; e; e = e->next) if (E->cap && h[e->to] + 1 < h[x]) {h[x] = h[e->to] + 1;cur[x] = e;}cnt[h[x]]++;if (x! = S) x = p[x]->rev->to;}}return flow;}#define ID (i, j) ((i) * m + (j))void init () {int n, m; scanf ("%d%d", &n, &m);S = n * m; T = S + 1; N = T + 1;for (int i = 0; i < n; i++)For (int j = 0; J < m; j + +)scanf ("%d", Mp[i] + j);for (int i = 0; i < n; i++)For (int j = 0; J < m; J + +) {if (mp[i][j] = = 1) Addedge (S, id (i, j), INF); if (mp[i][j] = = 2) Addedge (ID (i, j), T, INF);if (i-1 >= 0 && (mp[i][j]! = Mp[i-1][j] | |! ( MP[I][J] | MP[I-1][J] )))Addedge (ID (i, j), ID (i-1, j), 1);if (i + 1 < n && (mp[i][j]! = mp[i + 1][j] | |! ( MP[I][J] | Mp[i + 1][j] )))Addedge (ID (i, j), ID (i + 1, j), 1);if (j-1 >= 0 && (mp[i][j]! = Mp[i][j-1] | |! ( MP[I][J] | MP[I][J-1] )))Addedge (ID (i, j), ID (i, j-1), 1); if (j + 1 < m && ((mp[i][j]! = mp[i][j + 1]) | | MP[I][J] | mp[i][j+ 1] ))Addedge (ID (i, j), ID (i, j + 1), 1); }}int main () {init ();printf ("%d\n", Maxflow ());return 0;}
--------------------------------------------------------------------------
1412: [ZJOI2009] The story of wolves and Sheep time limit: Sec Memory Limit: 162 MB
Submit: 1851 Solved: 961
[Submit] [Status] [Discuss] Description "Wolf Love Sheep Ah love crazy, who let them really love A, wolf love sheep ah not absurd, they say there is love there is direction ... "Orez heard this song, thought: Wolf and sheep so harmonious, why not try the sheep and wolves together?" Do what you say! Orez can be seen as a n*m matrix lattice, the edge of the matrix has been put on the fence. But Drake soon found that wolves are also wolves, they are always watering to the sheep, the song is just a moving legend. So Orez decided to add some fences in the Sheep Wolf Circle, or to keep the wolves separately. Through careful observation, Orez found that wolves and sheep have their own territory, if wolves and sheep can not stay in their own territory, then they will become very irritable, not conducive to their growth. Orez want to add the fence as short as possible. Of course, the fence must first be guaranteed not to change the wolf sheep's territory, and then the fence has to be built intact, that is, must be built on the boundaries of the unit lattice and can not only build part. The first line of the input file contains two integers n and M. Next n rows of m integers per line, 1 means that the lattice belongs to the wolf's territory, 2 is the territory of the sheep, and 0 means that the lattice is not the territory of any animal. The output file contains only an integer ans, which represents the shortest length of the fence. Sample Input2 2
2 2
1 1
Sample Output2
Data range
10% of Data n,m≤3
30% of Data n,m≤20
100% of Data n,m≤100
HINT
Source
Bzoj 1412: [ZJOI2009] The story of the Wolf and the Sheep (min cut)