[POJ - 3255] Roadblocks (short-circuits Dijkstra's algorithm)

Roadblocks

Direct translation

Descriptions

Bessie moved to a new farm, and sometimes he would go back to see his old friend. But he did not want to go back soon, he likes to enjoy the scenery, so he will choose short-circuits, because she knew there must be a short circuit times.
This country has R (1 <= R <= 100000) two-way roads, each connecting N (1 <= N <= 5000) in two points. Bessie node No. 1, his friends house Input node number n is the first line: two integers N and R
Then R rows: Each line contains three integers, A, B, D, showing a connection between A and B output path length D of the short-circuits the output to the n-1

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (100 + 200 length = 300) and 1 -> 2 -> 3 -> 4 (100 + 250 + 100 length = 450)
Topic Link
 

Seek times shorter path from s to t, there are two cases: 1, the starting point s to a shortest vertex u + d (u, t). 2, short-circuits to a starting vertex u + d (u, t).

So when you need to update the path of the shortest path and a short path both times recorded.

Transmitting a set of data

 

4 2
1 2 100
2 4 200
The answer should be 500, but if the answer is initialized to 0 will output 700. 500 and because the result is 1 to 2, from 2 returning 1, then 2, then 4,100 + 100 + 100 + 200 = 500 is obtained; if initialized to 0 times the short side, the short secondary return path is no longer the source points, but between 2 and 4 folded, it will be too large.
 
AC Code
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>1
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define Mod 1000000007
#define eps 1e-6
#defineLong Long LL
 #define INF 0x3f3f3f3f
 #define MEM (X, Y) Memset (X, Y, the sizeof (X))
 #define MAXN. 5 + 5000
 #define P pair <int, int> // First second vertex numbers of the shortest path 
using  namespace STD;
 int N, R & lt;
 struct Edge 
{ 
    int to, DIS; 
    Edge ( int to, int DIS): to (to), DIS (DIS) {} 
}; 
Vector <Edge> G [MAXN]; // G [i] from i to G [i] .to a distance DIS 
int D [MAXN] [MAXN]; // D [i] [j] the shortest distance from i to j 
int D2 [MAXN] [MAXN]; //D [i] [j] from i to j times the short distance 
void Dijk ( int S) 
{ 
    The priority_queue <P, Vector <P>, Greater <P>> Q; // press the first team from small to large 
    for ( int = I 0 ; I <= N; I ++) // initializes s to all parts of the shortest, short-circuits are INF 
        D [s] [I] = INF, D2 [s] [I] = INF; 
    D [s ] [S] = 0 ; 
    q.push (P ( 0 , S));
     the while (! q.empty ()) 
    { 
        P P = q.top (); 
        q.pop (); 
        int V = p.second ; // point V 
        IF (D2 [S] [V] <p.first)// greater than twice the short path, certainly greater than the shortest path, and leave him 
            Continue ;
         for ( int I = 0 ; I <G [V] .size (); I ++ ) 
        { 
            Edge E = G [V] [I] ; // enumeration point adjacent the v 
            int TD = + e.dis p.first; // S + v to v, the distance to the distance e.to 
            IF (D [S] [e.to]> TD ) // S e.to the shortest path is less than dt, update D [S] [e.to] 
            { 
                the swap (D [S] [e.to], TD); 
                q.push (P (D [S] [e.to], e.to)); 
            } 
            IF (D [S] [e.to] <td && D2 [S] [e.to]> td) // td is greater than the shortest, less short-circuits, i.e. td short-circuits can be replaced 
            { 
                D2 [S] [e.to]= TD; 
                q.push (P (D2 [S] [e.to], e.to)); 
            } 
        } 
    } 
} 
int main () 
{ 
    the IOS; 
    CIN >> N >> R & lt;
     for ( int I = 0 ; I <R & lt; I ++ ) 
    { 
        int U, V, D; 
        CIN >> U >> >> V D; 
        G [U] .push_back (Edge (V, D)); 
        G [V] .push_back (Edge ( U, D)); 
    } 
    Dijk ( 1 ); // the shortest distance to an urban cities, the short-circuits 
    COUT << D2 [ 1 ] [N] << endl;
     return 0;
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11354367.html