Clever graph creation with a minimum cut of zoj 3475

Source: Internet
Author: User

For the first time, contact this type of graph creation question and record it.

Question:In a 20*20 square, some walls are built so that X and some A and E are not connected to the outside world, and the construction of the wall has a corresponding cost. For a qualified, corresponding income.

First, 2 ^ 5 refers to which a is to be enclosed, and then the minimum cost is required. This is a typical maximum flow least cut problem. X and a listed are connected to the same edge with the INF traffic, and E and the same edge with the INF traffic.

The above is a concise question of watashi. If I haven't responded to the problem at the beginning

Why is the minimum cut after creating a graph the minimum cost of separating X from some A and E? NOTE: If an edge is selected as a cut edge, the wall between two grids in the square is selected.

Note that the S set is X and A, and the T set is E and boundary. Therefore, the vertex in the s set after the minimum cut cannot reach the t set,

Therefore, it completes the function to enclose X and a and separate them from E.

 

View code

# Include <cstdio> # Include <Cstring> # Include < Set > # Include <Algorithm> Using   Namespace  STD;  Const   Int Max = 405  ;  Const   Int INF = 1000000000  ;  Struct  { Int  V, C, next;} edge [  2000 ], Edge [ 2000  ];  Int  E, head [Max], S, T;  Int  Gap [Max], cur [Max];  Int  Pre [Max], DIS [Max];  Void Add_edge ( Int S, Int T, Int C) {edge [e]. v = T; edge [e]. c = C; edge [e]. Next = Head [s]; head [s] = E ++ ; Edge [e]. v = S; edge [e]. c = C; edge [e]. Next = Head [T]; head [T] = E ++ ;}  Int Min ( Int A, Int B ){ Return (A =- 1 | B <)?B: ;}  Int SAP ( Int S, Int T, Int  N) {memset (gap,  0 , Sizeof  (GAP); memset (DIS,  0 , Sizeof  (DIS ));  Int  I;  For (I =0 ; I <n; I ++) cur [I] = Head [I];  Int U = pre [s] = s, maxflow = 0 , Aug =- 1  , V; Gap [  0 ] = N;  While (DIS [s] < N) {loop:  For (I = cur [u]; I! =- 1 ; I = Edge [I]. Next) {v =Edge [I]. V;  If (Edge [I]. c> 0 & Dis [u] = dis [v] + 1  ) {Aug = Min (Aug, edge [I]. c); Pre [v] = U; cur [u] = I; u = V;  If (U = T ){  For (U = pre [u]; V! = S; V = u, u =Pre [u]) {edge [cur [u]. c -= Aug; edge [cur [u] ^ 1 ]. C ++ = Aug;} maxflow + = Aug; Aug =- 1  ;}  Goto  Loop ;}}  Int Mindis = N;  For (I = head [u]; I! =- 1 ; I = Edge [I]. Next) {v = Edge [I]. V;  If (Edge [I]. c> 0 & Dis [v] < Mindis) {cur [u] = I; mindis = Dis [v] ;}}  If (-- Gap [dis [u]) = 0 ) Break  ; Gap [dis [u] = Mindis + 1 ] ++ ; U = Pre [u];}  Return  Maxflow ;}  Int  X [Max], Y [Max], Z [Max], H [Max], n, m;  Int ID ( Int X, Int Y ){ Return X * m + Y ;}  Int  Main (){ While (Scanf ( "  % D  " , & N, & M )! = EOF) {s = N * m; t = n * m + 1  ; Memset (Head, - 1 , Sizeof  (Head); e = 0  ;  For ( Int I =0 , F; I <= N; I ++ ){  For ( Int J = 0 ; J <m; j ++ ) {Scanf (  "  % D  " ,& F); add_edge (I = 0 ? T: ID (I- 1 , J), I = n? T: ID (I, j), f );} If (I = N) Break  ;  For ( Int J = 0 , F; j <= m; j ++ ) {Scanf (  "  % D  " ,& F); add_edge (J = 0 ? T: ID (I, j- 1 ), J = m?T: ID (I, j), f );}}  Int  K; scanf (  "  % D  " , & K ); Int T = K;  For ( Int I = 0 ; I <t; I ++ ) {Scanf (  "  % D  " , & Z [I], & X [I], & Y [I]);  If (Z [I] = 0  ) {Swap (Z [I], Z [  0  ]); Swap (Y [I], Y [  0  ]); Swap (X [I], X [  0  ]);}  Else   If (Z [I] < 0 ) {Add_edge (ID (X [I], Y [I]), T, INF); I --; T -- ;}} Memcpy (edge, edge,  Sizeof  (Edge); memcpy (H, Head,  Sizeof  (Head ));  Int Ans = INF;  Int TMP = E;  For ( Int I = 1 , F =0 ; I <( 1 <T); I + = 2  ) {F = 0  ; Memcpy (Head, H,  Sizeof  (H); memcpy (edge, edge,  Sizeof (Edge); E = TMP;  For ( Int J = 0 ; J <t; j ++){  If (I &( 1 < J) {add_edge (S, ID (X [J], Y [J]), INF); f -= Z [J] ;}} F + = SAP (S, T, N * m + 2  ); Ans = Min (ANS, f);} printf (  "  % D \ n  "  , ANS );} 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.