poj2679 Adventurous Driving Shortest circuit

Source: Internet
Author: User

http://poj.org/problem?id=2679after writing this question, the heart is collapsed, write a puzzle to commemorate

Test instructions

Give you n points M edge, start is St, end is end, give you some edge, these sides with cost and length, let you ask from start to end of the minimum cost, and meet the minimum cost to meet the minimum length.

The input (U,V,W1[LEN]W2) indicates that u to V has a cost of W1, the length of Len, and V to U has a cost of W2, with a length of Len on the side

That's probably the idea.

1. Delete the edges first, and keep the least connected side of the point

Specific implementation:

if (first[st]! = -1&&val > A[FIRST[ST]].W) return;

If you find that the weight of the previous edge connected by the current St is smaller than the weight of the current insert edge, return without joining this edge

if (first[st]! = -1&&val < A[FIRST[ST]].W) First[st] =-1;

In contrast, the previous edge is connected to a weight greater than the current value of the inserted weight, the first array of the period is connected to the empty node, making it inaccessible to the large side of the weight value

2.SPFA see whether the starting point can reach the end, can not reach the output void, to [and no ring] direct output of the minimum cost and length

3. Then deal with the case of negative ring , to determine whether the road from St to end has a ring, if the direct SPFA can only judge whether there is a ring on the graph, rather than the road of St to end whether there is a ring

① First we know the CNT array [records a point in the queue number, when the number of enqueue is greater than the n description by the negative ring]

② if the CNT of this point is greater than N, the St can go to this node, and this point is in a ring

③ to the point in the ② bfs[or DFS] if this point can reach end to indicate that the point is on the ring and the end is also on the ring, then you can output unbound

④ If none of the above meet the output minimum cost and length

PS: I don't have to build an anti-edge method.

1#include <stdio.h>2#include <queue>3#include <string.h>4 #defineINF 1000105 #defineMAXN 500106#include <algorithm>7 using namespacestd;8 intFIRST[MAXN],Get[MAXN], VIS[MAXN], CONT[MAXN], DIS[MAXN], LE[MAXN], flag;9 intN, M, St, end, Tot, circle;Ten structnode One { A     intu, V, W, Len, next; - }A[MAXN]; - voidAddedge (intStintEndintValintlen) the { -     if(first[st]! =-1&&val > A[FIRST[ST]].W)return; -     if(first[st]! =-1&&val < A[FIRST[ST]].W) First[st] =-1; -a[++tot].u = ST;A[TOT].V=END;A[TOT].W = Val;a[tot].len =Len; +A[tot].next = first[st];first[st] =tot; - } + voidBFsints) A { atqueue<int>Q; - Q.push (s); -     Get[s] =1; -      while(!q.empty ()) -     { -         intU =Q.front (); Q.pop (); in          for(inte = First[u]; E! =-1; E =a[e].next) -         { to             intv =a[e].v; +             if(!Get[v]) -             { the                 Get[V] =1; * Q.push (v); $             }Panax Notoginseng             if(v = =end) -Circle =1; the         } +     } A } the intSPFA () + { -queue<int>Q; $      for(inti =0; I <= N; i++) Dis[i] = INF, vis[i] =0; $      for(inti =0; I <= N; i++) Le[i] =INF; - Q.push (ST); -LE[ST] =0; theDIS[ST] =0; -VIS[ST] =1; cont[st]++;Wuyi      while(!q.empty ()) the     { -         intU = Q.front (); Q.pop (); Vis[u] =0; Wu          for(inte = First[u]; E! =-1; E =a[e].next) -         { About             intv =a[e].v; $             if(DIS[U]+A[E].W < dis[v]| | (DIS[U]+A[E].W = = Dis[v]&&le[u]+a[e].len <Le[v])) -             { -DIS[V] = Dis[u] +A[E].W; -LE[V] = Le[u] +A[e].len; A                 if(!Vis[v]) +                 { the Q.push (v); -VIS[V] =1; $cont[v]++; the                  } the             } the             if(Cont[v] > n+1) the             { -                 return 1; in             } the         } the     } About     return 0; the } the intMain () the { +     intx, Y, W1, W2, Len; -      while(~SCANF ("%d %d%d%d", &n, &m, &st, &end)) the     {BayiFlag =1; tot =0; thememset (First,-1,sizeof(first)); thememset (cont,0,sizeof(cont)); -          for(inti =1; I <= m; i++) -         { thescanf"(%d,%d,%d[%d]%d)", &x, &y, &AMP;W1, &len, &W2); the Addedge (x, Y, W1, Len), Addedge (y, X, W2, Len); the         } theFlag =SPFA (); -         if(Dis[end] = =INF) theprintf"void\n"); the         Else if(!flag) {printf ("%d%d\n", Dis[end], le[end]);Continue;} the         Else {94  the              for(inti =0; I < n; i++) the             { the                 if(Cont[i] >N) {98MemsetGet,0,sizeof(Get)); AboutCircle =0; - BFS (i);101                     if(circle) Break;102                 }103             }104             if(Circle | | cont[end] >N) theprintf"unbound\n");106             Elseprintf"%d%d\n", Dis[end], le[end]);107         }108     }109}
View Code

poj2679 adventurous Driving Shortest path

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.