POJ 3255 Roadblocks (secondary short path + Dijkstra algorithm)

Source: Internet
Author: User

Roadblocks

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7982 Accepted: 2921

Description

Bessie have moved to a small farm and sometimes enjoys returning to visit one of hers best friends. She does not want-to get-to-her-old home too quickly, because she likes the scenery along the. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1≤r≤100,000) bidirectional roads, each linking both of the N (1≤n≤5000) intersection s, conveniently numbered 1..N. Bessie starts at intersection 1, and she friend (the destination) is at intersection N.

The Second-shortest path may share roads with any of the shortest paths, and it could backtrack i.e., use the same road or I Ntersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path (s) (i.e., if EST paths exist, the second-shortest path is the one whose length are longer than those but no longer than any other path).

Input line 1:two space-separated integers:n and R
Lines 2..  R+1:each line contains three space-separated integers:a, B, and D that describe a road that connects intersections a and B and has length D (1≤d≤5000)

Output line 1:the length of the second shortest path between Node 1 and node N

Sample Input

4 4
1 2 2 4 +
2 3 +
3 4 100

Sample Output

450

4 (length 100+200=300) and 1, 2, 3 (length 100+250+100=450), 2, Hint, routes:1

Source Usaco 2006 November Gold



Test instructions: There is a block of R Road, N intersection. Roads can be used in both directions. Q: What is the length of the short path to the intersection of number 1th to n? The secondary short path is the second shortest path in all paths. And the same side can go through more than one time.


Parsing: The shortest path length is d[], the second short path length is dd[], then d[v] = min (D[u] + cost[u][v], Dd[u] + cost[u][v]), so we only need to calculate the shortest path and the second short path. This is not the same as the shortest path, in the implementation of the Dijkstra algorithm, we need to record the shortest path, but also to record the second short path. See Code

PS: The first time to write the form of adjacency table to write the shortest way, really learned. The adjacency table is much better than the time complexity of the adjacency matrix, which is to write a little trouble, but get used to it ^_^




AC Code:

#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <queue
> Using namespace std;
#define MAX_N 5005 #define INF 500000001 int N, R;
struct edge{int to, cost;};            Vector<edge> G[max_n];          An adjacency table of an d[max_n graph denotes an int [], dd[max_n];
    D[] Shortest path, dd[] time short-circuit void solve () {typedef pair<int, int> P;       Priority_queue<p, Vector<p>, greater<p> > Q;                                 Priority Queue Optimization Dijkstra fill (d, D+n, INF);
    The same function as memset, initialize array fill (dd, dd+n, INF);
    D[0] = 0;

    Q.push (P (0, 0));
        while (!q.empty ()) {p p = q.top (); Q.pop ();
        int v = p.second, D1 = P.first;
        if (Dd[v] < D1) continue;
            for (int i=0; i<g[v].size (); i++) {Edge e = g[v][i];
            int d2 = D1 + e.cost;
                if (D[e.to] > D2) {//Update shortest Path Swap (d[e.to], D2);
    Q.push (P (d[e.to], e.to));        } if (Dd[e.to] > D2 && d[e.to] < D2) {//update secondary short circuit dd[e.to] = D2;
            Q.push (P (dd[e.to], e.to));
}}} printf ("%d\n", dd[n-1]);
    } int main () {#ifdef sxk freopen ("In.txt", "R", stdin);
    #endif//Sxk int to, from, cost;
    Edge Foo; while (scanf ("%d%d", &n, &r)!=eof) {for (int i=0; i<r; i++) {scanf ("%d%d%d", &from, &
            To, &cost);  From--;                       To--; Number replaced by 0~n-1 foo.to = to;
            Foo.cost = Cost;
            G[from].push_back (foo);                         Foo.to = from;                  
        Bidirectional Edge G[to].push_back (foo);
    } solve ();
} return 0;
 }




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.