After understanding the meaning of the question, we will find that this question is the same as that of the poj 3469 dual-core processor.
The first item of the formula only works when xi = 0, the second item only works when xi = 1, and the third item only works when XI and XJ are not equal.
For each vertex I, XI is either 0 or 1, that is, vertex I belongs to the s set or t set. If vertex I and j are not in the same set, there will be additional value between them
The total minimum value corresponds to the capacity of a minimum cut. value = sum (v0i) + sum (v1j) + sum (Vij) makes V0 the source point and V1 the sink point, then I indicates that vertex I belongs to the s set, and vertex J indicates that J belongs to the t set. When (S, I) is a cut edge, (I, T) is not a cut edge. Likewise, when (I, T) is a cut edge, (S, I) is not a cut edge. When I and j are in the same set, (I, j) is not a cut edge.
1 <= I <j <= N, J belongs to n (I), but J belongs to n (I) it indicates the left and right adjacent points of vertex I and the upper and lower adjacent points. However, due to j> I, only the right adjacent points and the lower adjacent points can be obtained (if any). In the first place, WA several times.
In addition to the last set of test data, each group must end with one more blank line. At the beginning, only one line break is played. During debugging, it seems that a line is created, actually, the row would have to output the result, but some of them had to read the data first and leave the row blank, so they had to create another line break and break .. This PE is twice
Code:
# Include <iostream> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <math. h> <br/> # include <memory. h> <br/> using namespace STD; <br/> const int INF = 1 <30; <br/> const int max = 505; <br/> struct node <br/> {<br/> int V, C, next; <br/>} G [Max * max * 2]; <br/> int dis [Max], num [Max], cur [Max], pre [Max], adj [Max], map [Max] [Max]; <br/> int S, T, E, vn; <br/> void add (int u, int V, int C) <br/>{< br/> G [e]. V = V; G [e]. C = C; G [e]. next = adj [u]; adj [u] = e ++; <br/> G [e]. V = u; G [e]. C = 0; G [e]. next = adj [v]; adj [v] = e ++; <br/>}< br/> int ABS (int x) <br/>{< br/> return x> 0? X:-X; <br/>}< br/> int SAP () <br/>{< br/> int I, U, V, flow = 0, aug = inf, flag; <br/> for (I = 0; I <= Vn; I ++) <br/> {<br/> dis [I] = num [I] = 0; <br/> cur [I] = adj [I]; <br/>}< br/> num [0] = Vn; <br/> pre [s] = u = s; <br/> while (DIS [s] <VN) <br/>{< br/> flag = 0; <br/> for (I = cur [u]; i! =-1; I = G [I]. next) <br/> {<br/> V = G [I]. v; <br/> If (G [I]. C & dis [u] = dis [v] + 1) <br/> {<br/> flag = 1; <br/> pre [v] = u; <br/> cur [u] = I; <br/> Aug = Aug <G [I]. c? Aug: G [I]. c; <br/> U = V; <br/> If (u = T) <br/>{< br/> flow ++ = Aug; <br/> while (u! = S) <br/>{< br/> U = pre [u]; <br/> G [cur [u]. c-= Aug; <br/> G [cur [u] ^ 1]. c + = Aug; <br/>}< br/> Aug = inf; <br/>}< br/> break; <br/>}< br/> If (FLAG) <br/> continue; <br/> If (-- num [dis [u] = 0) <br/> break; <br/> for (DIS [u] = Vn, I = adj [u]; I! =-1; I = G [I]. next) <br/> {<br/> V = G [I]. v; <br/> If (G [I]. C & dis [v] <dis [u]) <br/>{< br/> dis [u] = dis [v]; <br/> cur [u] = I; <br/>}< br/> dis [u] ++; <br/> num [dis [u] ++; <br/> U = pre [u]; <br/>}< br/> return flow; <br/>}< br/> int main () <br/> {<br/> int I, J, K, T, CA, V0, V1, R, col; <br/> scanf ("% d", & T); <br/> for (CA = 1; Ca <= T; CA ++) <br/>{< br/> scanf ("% d", & R, & Col, & v0, & V1 ); <br/> E = 0; <br/> memset (adj,-1, sizeof (adj); <br/> memset (MAP, 0, sizeof (MAP); <br/> S = 0; <br/> T = r * Col + 1; <br/> VN = t + 1; <br/> for (I = 1; I <= r; I ++) <br/> {<br/> for (j = 1; j <= Col; j ++) <br/>{< br/> scanf ("% d", & map [I] [J]); <br/>}< br/> for (I = 1; I <= r; I ++) <br/> {<br/> for (j = 1; j <= Col; j ++) <br/> {<br/> K = (I-1) * Col + J; <br/> Add (S, K, ABS (Map [I] [J]-V0); <br/> Add (K, S, ABS (Map [I] [J]-V0); <br/> Add (K, T, ABS (Map [I] [J]-V1 )); <br/> Add (T, K, ABS (Map [I] [J]-V1); <br/>/* if (I> 1) <br/>{< br/> Add (K, K-Col, ABS (Map [I] [J]-map [I-1] [J]); <br/> Add (k-Col, K, ABS (Map [I] [J]-map [I-1] [J]); <br/>}*/<br/> if (I <r) <br/> {<br/> Add (K, K + Col, ABS (Map [I] [J]-map [I + 1] [J]); <br/> Add (K + Col, K, ABS (Map [I] [J]-map [I + 1] [J]); <br/>}< br/>/* If (j> 1) <br/> {<br/> Add (K, K-1, ABS (Map [I] [J]-map [I] [J-1]); <br/> Add (K-1, K, ABS (Map [I] [J]-map [I] [J-1]); <br/>}*/<br/> If (j <col) <br/> {<br/> Add (K, k + 1, ABS (Map [I] [J]-map [I] [J + 1]); <br/> Add (k + 1, K, ABS (Map [I] [J]-map [I] [J + 1]); <br/>}< br/> printf ("case % d:/N", CA ); <br/> printf ("% d/N", SAP (); <br/> If (Ca <t) <br/> printf ("/N "); <br/>}< br/> return 0; <br/>}