dijkstra求次短路

思路:
在原来的d数组上增加d2数组保存次短路。
求从s到t的次短路径有两种情况:
1、起点s到某个顶点u的最短路+d(u,t)。
2、起点到某个顶点u的次短路+d(u,t)。

修改后的dijkstra:

void dijkstra(int st) {
    fill(d1, d1 + n + 1, INF);
    fill(d2, d2 + n + 1, INF);
    priority_queue<P> q;
    d1[st] = d2[st] = 0;
    q.push(P(0, st));
    while (q.size()) {
        P tmp = q.top();
        q.pop();
        //如果次短路都小于保存的路径值,则直接抛弃
        if (d2[tmp.second] < tmp.first)
            continue;
        //遍历
        for (auto i : map[tmp.second]) {
        //从当前点走到i的距离
            int d_tmp = i.cost + tmp.first;
            //修改最短路
            if (d1[i.to] > d_tmp) {
                swap(d1[i.to], d_tmp);
                q.push(P(d1[i.to], i.to));
            }
            //最短路修改完了以后修改次短路
            if (d2[i.to] > d_tmp && d1[i.to] < d_tmp) {
                d2[i.to] = d_tmp;
                q.push(P(d2[i.to], i.to));
            }
        }
    }
}

例题:POJ3255
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. 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 two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1…N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is 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 100
2 4 200
2 3 250
3 4 100
Sample Output
450

代码:

#include 
#include 
#include 
#include 
using namespace std;
struct edge {
    int to, cost;
    edge(int t, int c) { to = t, cost = c; }
};
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
vector<edge> map[1111];
int d1[1111], d2[1111], n, m;

void dijkstra(int st) {
    fill(d1, d1 + n + 1, INF);
    fill(d2, d2 + n + 1, INF);
    priority_queue<P> q;
    d1[st] = d2[st] = 0;
    q.push(P(0, st));
    while (q.size()) {
        P tmp = q.top();
        q.pop();
        if (d2[tmp.second] < tmp.first)
            continue;
        for (auto i : map[tmp.second]) {
            int d_tmp = i.cost + tmp.first;
            if (d1[i.to] > d_tmp) {
                swap(d1[i.to], d_tmp);
                q.push(P(d1[i.to], i.to));
            }
            if (d2[i.to] > d_tmp && d1[i.to] < d_tmp) {
                d2[i.to] = d_tmp;
                q.push(P(d2[i.to], i.to));
            }
        }
    }
}
int main() {
    cin >> n >> m;
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        map[a].push_back(edge(b, c));
        map[b].push_back(edge(a, c));
    }
    dijkstra(1);
    cout << (d2[n] == INF ? -1 : d2[n]) << endl;
    return 0;
}

你可能感兴趣的:(#,图论)