SGU 185 Two shortest short-circuit deletion edge optimization memory + network stream

Source: Internet
Author: User

Question link: Click the open link

Question:

Given n vertices, m records have no direction edge and Edge Weight (no duplicate edge)

Find two shortest paths that do not overlap from 1-N points (the two paths must be the shortest path)

First, we ran with the cost.

Then, the shortest path is optimized, and, and all edges in the shortest path are deleted.

Then you can actually run the network stream, and, at the beginning, it is still mle. Later, we removed the from the adjacent table ,,


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; # define ll int # define N 402 # define M 121000 # define inf 10737418 # define inf64 1152921504606846976 struct Edge {ll to, cap, nex ;} edge [M * 2]; // note that this must be large enough. Otherwise, re will have reverse arc ll head [N], edgenum; void add (ll u, ll v, ll cap, ll rw = 0) {// if it is a directed edge, add (u, v, cap); if it is a undirected edge, add (u, v, cap, cap); Edge E = {v, cap, head [u]}; edge [edgenum] = E; head [u] = edgenum ++; Edge E2 = {u, rw, head [v]}; edge [edgenum] = E2; head [v] = edgenum ++;} ll sign [N]; bool BFS (ll from, ll) {memset (sign,-1, sizeof (sign); sign [from] = 0; queue
       
         Q; q. push (from); while (! Q. empty () {int u = q. front (); q. pop (); for (ll I = head [u]; I! =-1; I = edge [I]. nex) {ll v = edge [I]. to; if (sign [v] =-1 & edge [I]. cap) {sign [v] = sign [u] + 1, q. push (v); if (sign [to]! =-1) return true ;}}return false;} ll Stack [N], top, cur [N]; ll Dinic (ll from, ll) {ll ans = 0; while (BFS (from, to) {memcpy (cur, head, sizeof (head); ll u = from; top = 0; while (1) {if (u = to) {ll flow = inf, loc; // loc indicates the minimum cap edge in the Stack for (ll I = 0; I <top; I ++) if (flow> edge [Stack [I]. cap) {flow = edge [Stack [I]. cap; loc = I ;}for (ll I = 0; I <top; I ++) {edge [Stack [I]. c Ap-= flow; edge [Stack [I] ^ 1]. cap + = flow;} ans + = flow; top = loc; u = edge [Stack [top] ^ 1]. to;} for (ll I = cur [u]; I! =-1; cur [u] = I = edge [I]. nex) // cur [u] indicates the subscript if (edge [I] of the edge where the u is located. cap & (sign [u] + 1 = sign [edge [I]. to]) break; if (cur [u]! =-1) {Stack [top ++] = cur [u]; u = edge [cur [u]. to;} else {if (top = 0) break; sign [u] =-1; u = edge [Stack [-- top] ^ 1]. to ;}} return ans;} void init () {memset (head,-1, sizeof head); edgenum = 0;} ll dis [N]; ll n, m; ll mp [401] [401]; bool inq [N]; void spfa (int from, int to) {for (int I = 1; I <= n; I ++) dis [I] = inf; memset (inq, 0, sizeof inq); dis [from] = 0; queue
        
          Q; q. push (from); inq [to] = 1; while (! Q. empty () {int u = q. front (); q. pop (); inq [u] = 0; for (int I = 1; I <= n; I ++) if (dis [I]> dis [u] + mp [u] [I]) {dis [I] = dis [u] + mp [u] [I]; if (! Inq [I]) inq [I] = 1, q. push (I) ;}} void dfs (ll u, ll fa) {if (u = n) {printf ("% d \ n", u ); return;} else printf ("% d", u); for (ll I = head [u]; ~ I; I = edge [I]. nex) {if (edge [I ^ 1]. cap! = 1 | (I & 1) continue; ll v = edge [I]. to; if (v = fa) continue; edge [I ^ 1]. cap = 0; dfs (v, u); return ;}} int main () {ll u, v, cost; while (~ Scanf ("% d", & n, & m) {init (); for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) mp [I] [j] = inf; while (m --) {scanf ("% d", & u, & v, & cost ); mp [u] [v] = mp [v] [u] = min (mp [u] [v], cost);} spfa (1, n ); if (dis [n] = inf) {puts ("No solution"); continue;} for (ll I = 1; I <= n; I ++) for (ll j = 1; j <= n; j ++) if (mp [I] [j]! = Inf & dis [j] = mp [I] [j] + dis [I]) add (I, j, 1); add (n, n + 1, 2); ll DIS = Dinic (1, n + 1); if (DIS! = 2) {puts ("No solution"); continue;} else {dfs (); dfs () ;}} return 0 ;} /* 4 6 1 2 1 1 3 1 3 3 3 3 4 2 2 4 1 1 1 1 3 1 3 1 3 3 3 3 3 1 2 2 2 2 3 3 1 3 1 */
        
       
      
     
    
   
  
 


Related Article

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.