HDU 1733 escape

Source: Internet
Author: User

Hdu_1733

Layered graph network flow. To ensure that only one person is standing at a place every second, you can split each vertex I into I and I 'and connect one side with the same capacity. In a layered graph, each layer represents a one-second position, and the number of layers in the graph is constantly increased until the maximum flow is equal to the number of people to get the minimum time.

In the case of-1, you can use search preprocessing at the beginning.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxn 20 # Define Maxd 60010 # Define Maxm 420010 # Define INF 0x3f3f3f Int  N, m, first [maxd], E, next [maxm], V [maxm], flow [maxm], CNT;  Int  S, T, d [maxd], Q [maxd], work [maxd], vis [maxn] [maxn], num;  Int DX [] = {- 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 ,- 1 , 1 };  Char  B [maxn] [maxn];  Void Add ( Int X, Int Y, Int  Z) {v [E] = Y, flow [e] = Z; next [E] = First [X], first [x] = e ++ ;}  Int Inside ( Int X, Int  Y ){ Return X> = 1 & X <= N & Y> = 1 & Y <= M ;}  Void  Init (){  Int  I, j, X;  For (I = 1 ; I <= N; I ++) scanf ( "  % S  " , B [I] + 1 ); S = 0 , T = 1  ; Memset (first, - 1 , Sizeof (First), E = 0  ;  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++ ) If (B [I] [J] = '  X  '  ) {X = N * m + I * m + J; add (S, X,  1 ), Add (x, S, 0  );}}  Void DFS ( Int X, Int  Y ){  Int I, NX, NY; vis [x] [Y] = 1  ;  For (I = 0 ; I < 4 ; I ++ ) {NX = X + dx [I], NY = Y + Dy [I];  If (Inside (NX, NY) & B [NX] [NY]! = '  #  ' &&! Vis [NX] [NY]) DFS (NX, NY );}} Void Build ( Int  Cur ){  Int I, J, K, Ni, NJ, D = 2 * N * m * Cur, X, Y;  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++ )  If (B [I] [J]! = ' #  '  ) {X = D + I * m + J, y = x + N * M; add (x, y,  1 ), Add (Y, X, 0  );  If (B [I] [J] = '  @  ' ) Add (Y, T, 1 ), Add (T, y, 0  ); Y = X-N *M; add (Y, X,  1 ), Add (x, y, 0  );  For (K = 0 ; K < 4 ; K ++ ) {Ni = I + dx [K], nj = J + Dy [k];  If (Inside (Ni, NJ) & B [Ni] [NJ]! = '  #  ' ) {X = D-N * m + I * m + J, y = d + Ni * m + NJ; add (x, y,  1 ), Add (Y, X, 0  );}}}}  Int  BFS (){  Int I, j, rear = 0  ; Memset (D, - 1 , Sizeof (D [0 ]) * CNT); 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 ]) *CNT );  While (T = DFS (S, INF) ans + = T ;}  Return  Ans ;}  Void  Solve (){  Int  I, j, ans; memset (VIS,  0 , Sizeof  (VIS); num = 0  ;  For (I =1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++ ){  If (B [I] [J] = '  @  ' &&! Vis [I] [J]) DFS (I, j );  If (B [I] [J] = '  X  ' ) ++Num ;}  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++ )  If (B [I] [J] = '  X  ' &&! Vis [I] [J]) {printf (  "  -1 \ n "  );  Return  ;}  If (Num = 0  ) {Printf (  "  0 \ n  "  );  Return  ;}  For (I = 1 , ANS = 0 , CNT = N * m * 2 + M + 1 ; I ++ ) {Build (I); CNT + = N * m * 2  ; Ans + = Dinic ();  If (ANS = num) Break  ;} Printf (  "  % D \ n  "  , I );}  Int Main (){  While (Scanf ( "  % D  " , & N, & M) = 2  ) {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.