Hdu1569: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1569
Question: Chinese.
Problem: Classic problems. First, divide all vertices into two parts: X and Y according to (I + J) % 2 = 1 and (I + J) % 2 = 0. For the X, and y parts respectively, any two numbers are not adjacent, so they can be arbitrarily removed. The number of queries in Question X and the number of queries in question y are the final sum and maximum values. Therefore, if I and j are adjacent, one side is established between I and j, And the capacity is infinite. The X part and the source point establish one side, and the y part establish one side with the sink point. In this way, the network flow is the maximum flow, that is, the minimum cut. Combined with the definition of the minimum cut, it is obvious that the final answer is "Total" and "-maxflow.
1 # include <iostream> 2 # include <cstring> 3 # include <algorithm> 4 # include <cstdio> 5 # include <queue> 6 # define INF 100000000 7 using namespace STD; 8 const int n = 2612; 9 const int M = 14820; 10 struct node {11 int V; 12 int f; 13 int next; 14} edge [m]; 15 int n, m, U, V, CNT, Sx, ex; 16 int head [N], pre [N]; 17 int MP [51] [51]; // apply for 18 void Init () {19 CNT = 0; 20 memset (Head,-1, sizeof (head); 21} 22 void a as required Dd (int u, int V, int W) {23 edge [CNT]. V = V; 24 edge [CNT]. F = W; 25 edge [CNT]. next = head [u]; 26 head [u] = CNT ++; 27 edge [CNT]. f = 0; 28 edge [CNT]. V = u; 29 edge [CNT]. next = head [v]; 30 head [v] = CNT ++; 31} 32 bool BFS () {33 memset (PRE, 0, sizeof (pre )); 34 pre [SX] = 1; 35 queue <int> q; 36 Q. push (SX); 37 while (! Q. Empty () {38 int d = Q. Front (); 39 Q. Pop (); 40 for (INT I = head [d]; I! =-1; I = edge [I]. Next) {41 if (edge [I]. F &&! Pre [edge [I]. v]) {42 Pre [edge [I]. v] = pre [d] + 1; 43 Q. push (edge [I]. v); 44} 45} 46} 47 return pre [Ex]> 0; 48} 49 int dinic (INT flow, int PS) {50 int F = flow; 51 if (PS = ex) return F; 52 for (INT I = head [PS]; I! =-1; I = edge [I]. next) {53 If (edge [I]. F & Pre [edge [I]. v] = pre [PS] + 1) {54 int A = edge [I]. f; 55 int T = dinic (min (A, flow), edge [I]. v); 56 edge [I]. f-= T; 57 edge [I ^ 1]. F + = T; 58 flow-= T; 59 If (flow <= 0) break; 60} 61 62} 63 If (F-flow <= 0) pre [PS] =-1; 64 return F-flow; 65} 66 int solve () {67 int sum = 0; 68 while (BFS ()) 69 sum + = dinic (INF, SX); 70 return sum; 71} 72 int main () {73 int sum = 0; 74 while (~ Scanf ("% d", & N, & M) {75 Init (); sum = 0; 76 for (INT I = 1; I <= N; I ++) {77 for (Int J = 1; j <= m; j ++) {78 scanf ("% d ", & amp; MP [I] [J]); 79 sum + = MP [I] [J]; 80 If (I + J) % 2 = 0) {81 add (0, (I-1) * m + J, MP [I] [J]); 82} 83 else {84 add (I-1) * m + J, N * m + 1, MP [I] [J]); 85} 86} 87} 88 for (INT I = 1; I <= N; I ++) {89 for (Int J = 1; j <= m; j ++) {90 if (I + J) % 2 = 0) {91 int A = (I-1) * m + J; 92 if (I> 1) Add (a, a-m, INF); 93 if (I <n) add (A, A + M, INF); 94 If (j> 1) Add (A, A-1, INF); 95 If (j <m) add (A, INF, A + 1, INF); 96} 97} 98} 99 SX = 0, Ex = N * m + 1; 100 printf ("% d \ n ", sum-solve (); 101} 102 return 0; 103}
View code
Number of squares (2)