Title: hdoj 3657 game
Category:
Question: Give a matrix with values. Set a number according to the rule to maximize the score. The rule is as follows:
1: Some grids must be obtained. The score is the sum of the obtained grids.
2: subtract all 2 * (X | Y) from the adjacent lattice)
Analysis: This is a standard question for minimum cut. For this question, we recommend that you do hdoj 1659 first. Explanation: Click to open the link.
Similar to a grid question, it is restricted to adjacent ones. We can create a graph based on the general scheme of the grid question, and divide the graph into a Bipartite Graph Based on the row and column of the grid and the parity.
Now we need a maximum score. We can convert it to a minimum cut. After creating a graph, we need to cut at least the edge with the smallest weight so that there is no traffic from the source point S to the sink point T.
First, we know that there are some grids that must be taken, so we set the traffic to an infinite number when creating a graph, and then let the minimum cut off. Another condition is that the adjacent side is subtracted from a certain value. We can set the adjacent side size to the current value.
Graph creation scheme:
Set a super Source Vertex s and a super sink vertex t
Connects s to the odd side, and the capacity is the current grid value. If it is mandatory, the capacity INF
Connect t to an even point. The same capacity is the current value. If it is a required point, the capacity INF
Then, edge is created between adjacent points of each grid. The capacity is 2 * (X | Y)
Find the minimum cut, that is, the maximum stream. Then: ANS = sum-min cut of all grids
AC code:
# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> # include <vector> # include <queue> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int n = 3500; const int INF = 0x3f3f3f; int n, m; struct node {int from, to, Cap, flow ;}; vector <int> V [N]; vector <node> E; int vis [N]; // construct the hierarchy chart int cur [N]; void add_node (int from, int to, int cap) {e. push_back (node) {from, T O, Cap, 0}); E. push_back (node) {to, from, 0, 0}); int TMP = E. size (); V [from]. push_back (tmp-2); V [to]. push_back (tmp-1);} bool BFS (int s, int t) {del (VIS,-1); queue <int> q; q. push (s); vis [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <V [X]. size (); I ++) {node TMP = E [V [x] [I]; If (vis [TMP. to] <0 & TMP. cap> TMP. flow) // The second condition ensures {vis [TMP. to] = vis [x] + 1; q. push (TMP. to) ;}}} if (vis [T]> 0) return true; return false;} int DFS (int o, int F, int T) {If (O = T | f = 0) // returns F; int A = 0, ANS = 0; For (Int & I = cur [O]; I <V [O]. size (); I ++) // note the preceding '&', which is an important optimization {node & TMP = E [V [O] [I]; if (vis [TMP. To] = (vis [O] + 1) & (A = DFS (TMP. to, min (F, TMP. cap-tmp.flow), t)> 0) {TMP. flow + = A; E [V [O] [I] ^ 1]. flow-= A; // save graph mode ans + = A; F-= A; If (F = 0) // note break optimization;} return ans; // optimization} int dinci (int s, int t) {int ans = 0; while (BFS (S, T) {del (cur, 0 ); int TM = DFS (S, INF, T); ans + = TM;} return ans;} struct Nno {int X, OK ;}; NNo MP [60] [60]; int ID (int I, Int J) {return (I-1) * m + J;} int main () {int K; while (~ Scanf ("% d", & N, & M, & K) {int sum = 0; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & MP [I] [J]. x); MP [I] [J]. OK = 0; sum + = MP [I] [J]. X ;}for (INT I = 0; I <K; I ++) {int X, Y; scanf ("% d", & X, & Y); MP [x] [Y]. OK = 1;} int S = 0, T = m * n + 1; for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {If (I + J) % 2) {If (MP [I] [J]. OK) add_node (S, ID (I, j), INF); else add_node (S, ID (I, j), MP [I] [J]. x); if (I> 1) {int TMP = MP [I] [J]. X & MP [I-1] [J]. x; add_node (ID (I, j), ID (I-1, J), 2 * TMP);} If (j> 1) {int TMP = MP [I] [J]. X & MP [I] [J-1]. x; add_node (ID (I, j), ID (I, J-1), 2 * TMP);} if (I <n) {int TMP = MP [I] [J]. X & MP [I + 1] [J]. x; add_node (ID (I, j), ID (I + 1, J), 2 * TMP);} If (j <m) {int TMP = MP [I] [J]. X & MP [I] [J + 1]. x; add_node (ID (I, j), ID (I, j + 1), 2 * TMP) ;}} else {If (MP [I] [J]. OK) add_node (ID (I, j), T, INF); else add_node (ID (I, j), T, MP [I] [J]. x) ;}} printf ("% d \ n", Sum-dinci (S, T); For (INT I = 0; I <= T; I ++) V [I]. clear (); E. clear () ;}return 0 ;}
Hdoj 3657 game [minimum cut]