Hit 2713 matrix1

Source: Internet
Author: User

Hit_2713

The biggest one is equal to the least one left behind. Therefore, the problem becomes that the precious stones with the minimum value are not adjacent to each other.

First, each grid is dyed in black and white Based on the parity of I + J, and then s is connected to all the black grids. The capacity is the value of the corresponding grid, and then all the white grids are connected to T, capacity also corresponds to the value of the grid, and finally connects the Black Grid and the adjacent white grid, the capacity is INF.

The expected solution is a cut in this figure. The points that can be reached from S represent the white grids left behind and the black grids taken away, the point that can reach t represents the black lattice left behind and the white lattice taken away. In order to minimize the value of the remaining lattice, you can perform a minimum cut on the source image.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxd 2510# Define Maxm 15010 # Define INF 0x3f3f3f Int  N, m, first [maxd], E, next [maxm], V [maxm], flow [maxm];  Int  Sum, S, T, d [maxd], Q [maxd], work [maxd];  Int DX [] = {- 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 ,-1 , 1  };  Void Add ( Int X, Int Y, Int  Z) {v [E] = Y, flow [e] = Z; next [E] = First [X], first [x] = e ++ ;}  Void  Init (){  Int  I, J, K, X, Ni, NJ; scanf ( "  % D  " , & N ,& M); s = 0 , T = n * m + 1  ; Memset (first, - 1 , Sizeof (First [ 0 ]) * (T + 1  ); E = 0  ; Sum =0  ;  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++ ) {Scanf (  "  % D  " , & X), sum + = X;  If (I + J & 1 ) {Add (S, (I - 1 ) * M + J, x), add (I- 1 ) * M + J, S, 0  );  For (K = 0 ; K < 4 ; K ++ ) {Ni = I + dx [K], nj = J + Dy [k];  If (Ni> = 1 & Ni <= N & NJ> = 1 & NJ <= M) add (I - 1 ) * M + J, (Ni- 1 ) * M + NJ, INF), add (Ni- 1 ) * M + NJ, (I- 1 ) * M + J, 0  );}}  Else  Add (I - 1 ) * M + J, t, x), add (T, (I-1 ) * M + J, 0  );}}  Int  BFS (){  Int I, j, rear = 0  ; Memset (D, - 1 , Sizeof (D [ 0 ]) * (T + 1  ); D [s] = 0 , Q [rear ++] =S;  For (I = 0 ; I <rear; I ++ )  For (J = first [Q [I]; J! =- 1 ; J = Next [J])  If (Flow [J] & D [V [J] =- 1  ) {D [V [J] = D [Q [I] + 1 , Q [rear ++] = V [J];  If (V [J] = T) Return   1  ;}  Return   0  ;}  Int DFS ( Int Cur, Int  A ){  If (Cur = T)  Return  A;  For (Int & I = work [cur]; I! =- 1 ; I = Next [I])  If (Flow [I] & D [V [I] = d [cur] + 1  )  If ( Int T = DFS (V [I], STD: min (A, flow [I]) {flow [I] -= T, flow [I ^ 1 ] + = T;  Return T ;}  Return   0  ;}  Int  Dinic (){  Int Ans = 0  , T;  While  (BFS () {memcpy (work, first,  Sizeof (First [ 0 ]) * (T + 1  )); While (T = DFS (S, INF) ans + = T ;}  Return  Ans ;}  Void  Solve () {printf (  "  % D \ n  " , Sum- Dinic ());}  Int  Main (){  Int  T; scanf ( "  % D  " ,& T );  While (T -- ) {Init (); solve ();}  Return   0  ;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.