To solve the problem of short-circuit, the method is similar to finding the shortest single source, but the subject is the single source shortest circuit and the second shortest piece solution
To a certain point of the shortest path (EG:U): Assuming that the most short-circuit is s->v->u, the short-circuit is s->v->u ' or s->v '->u Note: S->v ' indicates a secondary short-circuit of s->v, v->u ' means v->u Secondary short Circuit
What needs to be done is to update the single source shortest and second shortest
If the current node is U, the node adjacent to U is V
1, if the node with u adjacent to the V, through u can minimize the distance of S to its, then update its shortest, and note the original S to v shortest circuit, in case of an updated short-circuit
2, a, if the path length of s through U to V is greater than the minimum path length of S to V, but less than S to V of the secondary short-circuit length, then update the second short path of S to V
b, if the original shortest circuit less than the short-circuit, update the secondary short-circuit
#include <iostream> #include <vector> #include <queue> #define MAXN 100010#define INF 9999999999using Namespace Std;typedef pair<int,int>p;struct Edge {int from; int to; Long long cost;}; int N,r;vector <edge> G[maxn];long long dist[maxn],dist2[maxn];void Solve () {priority_queue<p,vector<p >,greater<p> > que; Fill (Dist,dist+n,inf); Fill (Dist2,dist2+n,inf); Dist[0] = 0; Que.push (P (0,0)); while (!que.empty ()) {p pp = que.top (); Que.pop (); int v = pp.second; int d = Pp.first; if (Dist2[v] < D) continue; for (int i=0;i<g[v].size (); i++) {Edge &e = G[v][i]; int d2 = d+ e.cost; if (Dist[e.to] > D2) {int TP = dist[e.to]; Dist[e.to] = D2; D2 = TP; Que.push (P (dist[e.to],e.to)); } if (Dist2[e.to] > D2 && dist[e.to] <d2) { DIST2[E.TO]=D2; Que.push (P (dist2[e.to],e.to)); }}} cout<< Dist2[n-1]<<endl;} int main () {cin>>n>>r; Edge TMP,TMP1; int from,to,cost; for (int i=0;i<r;i++) {cin>>from>>to>>cost; cout<<tmp.from<<tmp.to<<tmp.cost; Tmp.from = from-1; tmp.to = to-1; Tmp.cost =cost; G[from-1].push_back (TMP); Tmp1.from = to-1; tmp1.to = from-1; Tmp1.cost = Cost; G[to-1].push_back (TMP1); } solve (); return 0;}
POJ-3255 roadblocks