Sgu 185 two shortest

Source: Internet
Author: User

Sgu_185

This problem is caused by serious dizziness of the memory card ......

In fact, the train of thought is still intuitive. Because two roads do not overlap, you only need to use the network stream, and add the two shortest paths, the minimum cost is the maximum flow, at last, check whether the cost of the two routes is twice the maximum short circuit. However, if the write is naked, because the undirected edge is equivalent to two directed edges, sorry, the memory burst ...... Open it up. Sorry, re ......

Dizzy ...... Let's change the network stream to write it. In this way, we can use dij to start from 1 to get the shortest path and then start from N to get the shortest path, traverse all the edges to know which edge is on the shortest path, and know the direction of the edge, then, based on the graph composed of edges on the shortest road, we can make the maximum flow. Finally, we can see whether the total traffic is 2.

Of course, the method of saving memory is found here. As long as you first find the edges on the shortest path, these edges will be directed, so the memory will be half left, at this time, the usage of the fee stream will not blow up the memory, but you will find that it seems that the cost stream is useless because the cost does not matter, they are all edges on the shortest road. Let's set the fee to 0. Forget it. Simply don't charge this array, so I finally wrote it, "done", and wrote it back to the network stream. The minimum fee is the maximum fee.AlgorithmIsn't it Ek? = _ =

However, it is worth mentioning that this cannot be done when the final path is output: first, first, store the largest stream, then store the augmented path, and then store the largest stream, then two augmented paths are output. Because the second augmented path may go through the reverse side, that is, it is equivalent to regretting a previous route. This is obviously incorrect, because it is equivalent to the fact that this edge has not been taken, but it is included in the two expansion paths. However, you can finally go to the end point based on the positive side of the residual quantity network along the positive side with the residual quantity 0. By the way, you can set the residual quantity to 1, so that the two paths are output after two times.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxd 410 # Define Maxm 160010 # Define INF 0x3f3f3f Int  N, m, G [maxd] [maxd], first [maxd], E, next [maxm], V [maxm], flow [maxm]; Int  S, T, Q [maxd], d [maxd], work [maxd], diss [maxd], DIST [maxd], pre [maxd];  Void  Init (){  Int  I, x, y, z; memset (G,  0x3f , Sizeof  (G ));  For (I = 0 ; I <m; I ++ ) {Scanf (  "  % D " , & X, & Y ,& Z); G [x] [Y] = G [y] [x] = Z ;}}  Void Dij ( Int S, Int T, Int * Dis ){  Int  I, J, K, min; memset (DIS,  0x3f , Sizeof (DISs [ 0 ]) * (N +1  ); Memset (D,  0 , Sizeof (D [ 0 ]) * (N + 1  ); DIS [s] = 0  ;  For  (;) {Min = INF;  For (I = 1 ; I <= N; I ++)If (! D [I] & dis [I] <min) min = dis [k = I];  If (Min = inf) Break  ; D [k] = 1  ;  For (I = 1 ; I <= N; I ++) dis [I] = STD: min (DIS [I], DIS [k] + G [k] [I]) ;}}  Int  BFS (){  Int I, j, rear =0  ; Memset (D, - 1 , Sizeof (D [ 0 ]) * (N + 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 ]) * (N + 1  ));  While (T = DFS (S, INF) ans + = T ;}  Return  Ans ;}  Void Add ( Int X, Int Y,Int  Z) {v [E] = Y, flow [e] = Z; next [E] = First [X], first [x] = e ++ ;}  Void  Print (){  Int I, cur = 1  ;  For  (;;){  If (Cur = T) {printf (  " % D \ n  "  , Cur );  Return  ;} Printf (  "  % D  "  , Cur );  For (I = first [cur]; I! =- 1 ; I = Next [I])  If (I & 1 ) = 0 & Flow [I] = 0  ) {Flow [I] = 1 , Cur = V [I];  Break  ;}}}  Void  Solve (){  Int  I, J; dij (  1 , N, diss), dij (n, 1  , DIST );  If (DISs [N] =INF) {printf (  "  No solution \ n  "  );  Return  ;} Memset (first, - 1 , Sizeof (First [ 0 ]) * (N + 1 ), E = 0  ;  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= N; j ++ )  If (I! = J & diss [I] + G [I] [J] + dist [J] = Diss [N]) add (I, j,  1 ), Add (J, I, 0  ); S = 0 , T = N; add (S,  1 , 2 ), Add ( 1 , S, 0  );  If (Dinic ()! = 2  ) Printf (  "  No solution \ n  "  );  Else  Print (), print ();}  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.