HDU 1565 1569 square fetch (maximum vertex weight independent set)
Question link
Question: Chinese
Idea: maximum vertex weight independent set = total weight-minimum cut = total weight-maximum stream
Therefore, the source vertex cannot be connected to edges, which can be divided into black and white boards. The Source vertex is connected to a black spot, and the white vertex is connected to a sink vertex. The capacity is the point capacity, and then the adjacent black and white areas are connected to an infinitely large edge.
Code:
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace std; const int MAXNODE = 2505; const int MAXEDGE = 100005; typedef int Type; const Type INF = 0x3f3f3f; struct Edge {int u, v; Type cap, flow; Edge () {} Edge (int u, int v, Type cap, Type flow) {this-> u = u; this-> v = v; this-> cap = cap; this-> flow = flow ;}}; struct Dinic {int n, m, s, t; Edge edges [MAXEDGE]; int first [MAXNOD E]; int next [MAXEDGE]; bool vis [MAXNODE]; Type d [MAXNODE]; int cur [MAXNODE]; vector <int> cut; void init (int n) {this-> n = n; memset (first,-1, sizeof (first); m = 0;} void add_Edge (int u, int v, Type cap) {edges [m] = Edge (u, v, cap, 0); next [m] = first [u]; first [u] = m ++; edges [m] = Edge (v, u, 0, 0); next [m] = first [v]; first [v] = m ++;} bool bfs () {memset (vis, false, sizeof (vis); queue <int> Q; Q. push (s); d [s] = 0; Vis [s] = true; while (! Q. empty () {int u = Q. front (); Q. pop (); for (int I = first [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (! Vis [e. v] & e. cap> e. flow) {vis [e. v] = true; d [e. v] = d [u] + 1; Q. push (e. v) ;}}return vis [t];} Type dfs (int u, Type a) {if (u = t | a = 0) return; type flow = 0, f; for (int & I = cur [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (d [u] + 1 = d [e. v] & (f = dfs (e. v, min (a, e. cap-e. flow)> 0) {e. flow + = f; edges [I ^ 1]. flow-= f; flow + = f; a-= f; if (a = 0) break;} return flow;} Type Maxflow (int s, int t) {this-> s = s; this-> t = t; Type flow = 0; while (bfs () {for (int I = 0; I <n; I ++) cur [I] = first [I]; flow + = dfs (s, INF);} return flow;} void MinCut () {cut. clear (); for (int I = 0; I < M; I + = 2) {if (vis [edges [I]. u] &! Vis [edges [I]. v]) cut. push_back (I) ;}} gao; const int N = 55; const int d [4] [2] = {0, 1, 0,-1, 1, 0, -1, 0}; int n, m, g [N] [N], sum; int main () {while (~ Scanf ("% d", & n, & m) {sum = 0; for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) {scanf ("% d", & g [I] [j]); sum + = g [I] [j];} gao. init (n * m + 2); for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) {if (I + j) % 2 = 0) {gao. add_Edge (0, I * m + j + 1, g [I] [j]); for (int k = 0; k <4; k ++) {int x = I + d [k] [0]; int y = j + d [k] [1]; if (x <0 | x> = n | y <0 | y> = m) continue; gao. add_Edge (I * m + j + 1, x * m + y + 1, g [I] [j] + g [x] [y]);} else gao. add_Edge (I * m + j + 1, n * m + 1, g [I] [j]);} printf ("% d \ n", sum-gao. maxflow (0, n * m + 1);} return 0 ;}
HDU 1565 1569 square fetch (maximum vertex weight independent set)