http://poj.org/problem?id=3255
Bessie sometimes visited her friends, but she did not want to go the quickest way home, but wanted to take a short circuit shorter than the shortest way.
The town consists of a two-way road of R, with n junctions. The number is 1 to n, what is the secondary short-circuit length of the intersection of number 1th to n? The secondary short circuit is
A short path shorter than the shortest short length. The same edge can go through multiple times.
A minor short-circuit to a vertex v is either handsome to the other vertex U's shortest path plus the u-v edge, or the short circuit to u, plus the u-v side,
So what is required is the shortest and minor short-circuiting to all vertices, so for each vertex we record more than the minimum distance,
There is a short distance, and then as long as the Dijkstra algorithm with the same approach, constantly update these two distances can be found in the secondary short-circuit.
1 /* ***********************************************2 Author:zch3 Created TIME:2015/5/13 8:16:454 File name:a.cpp5 ************************************************ */6 7#include <cstdio>8#include <cstring>9#include <iostream>Ten#include <algorithm> One#include <vector> A#include <queue> -#include <Set> -#include <map> the#include <string> -#include <cmath> -#include <cstdlib> -#include <ctime> + using namespacestd; - Const intMAXN =5010; + Const intINF =1000000000; A at intN, R; - structEdge { - intTo,cost; - Edge () {} -EdgeintXinty) { -to=x; incost=y; - } to }; +typedef pair<int,int>P; -Vector<edge>G[MAXN]; the intDIST[MAXN]; * intDIST2[MAXN]; $ Panax Notoginseng voidDijkstraints) { -priority_queue<p,vector<p>,greater<p> >que; the for(intI=0; i<=n;i++) dist[i]=INF; + for(intI=0; i<=n;i++) dist2[i]=INF; Adist[s]=0; theQue.push (P (0, s)); + - while(!Que.empty ()) { $P p=que.top (); Que.pop (); $ intV=p.second, d=P.first; - if(dist2[v]<d)Continue;//the second short distance of V is smaller than D. - for(intI=0; I<g[v].size (); + +i) { theEdge e=G[v][i]; - //cout<<e.to<<endl;Wuyi intD2=d+E.cost; the if(DIST[E.TO]>D2) {//Update Shortest distance - swap (DIST[E.TO],D2); Wu Que.push (P (dist[e.to],e.to)); - } About if(DIST2[E.TO]>D2&&DIST[E.TO]<D2) {//Update secondary short distance $dist2[e.to]=D2; - //cout<<d2<<endl; - Que.push (P (dist2[e.to],e.to)); - } A } + } theprintf"%d\n", Dist2[n]); - } $ intMain () the { the //freopen ("A.txt", "R", stdin); the //freopen ("B.txt", "w", stdout); the inta,b,c; -scanf"%d%d",&n,&R); in for(intI=0; i<r;++i) { thescanf"%d%d%d",&a,&b,&c); the G[a].push_back (Edge (B,c)); About G[b].push_back (Edge (A,c)); the } theDijkstra1); the return 0; +}
poj-3225 Roadblocks (secondary short circuit)