Dijkstra的问题是,找到所有的最小路径

Trouble with Dijkstra , finding all minimum paths

本文关键字:路径 小路 问题是 Dijkstra      更新时间:2023-10-16

我们遇到了一个问题,我们试图找到图中从一个节点到另一个节点的所有最短路径。我们已经实现了dijkstra,但是我们真的不知道如何找到它们。

我们一定要用BFS吗?

#include <vector>
#include <iostream>
#include <queue>
using namespace std;
typedef pair <int, int> dist_node;
typedef pair <int, int> edge;
const int MAXN = 10000;
const int INF = 1 << 30;
vector <edge> g[MAXN];
int d[MAXN];
int p[MAXN];
int dijkstra(int s, int n,int t){
    for (int i = 0; i <= n; ++i){
        d[i] = INF;  p[i] = -1;
    }
    priority_queue < dist_node, vector <dist_node>,greater<dist_node> > q;
    d[s] = 0;
    q.push(dist_node(0, s));
    while (!q.empty()){
        int dist = q.top().first;
        int cur = q.top().second;
        q.pop();
        if (dist > d[cur]) continue;
        for (int i = 0; i < g[cur].size(); ++i){
            int next = g[cur][i].first;
            int w_extra = g[cur][i].second;
            if (d[cur] + w_extra < d[next]){
                d[next] = d[cur] + w_extra;
                p[next] = cur;
                q.push(dist_node(d[next], next));
            }
        }
    }
    return d[t];
}
vector <int> findpath (int t){
    vector <int> path;
    int cur=t;
    while(cur != -1){
        path.push_back(cur);
        cur = p[cur];
    }
    reverse(path.begin(), path.end());
    return path;
}

这是我们的代码,我们相信我们必须修改它,但我们真的不知道在哪里

目前,您只保存/检索您碰巧找到的最短路径之一。考虑这个例子:

4 nodes
0 -> 1
0 -> 2
1 -> 3
2 -> 3

很明显,您不能为每个位置提供单个p[]值,因为事实上第4个节点(3)有两个先前有效的节点:12

因此,您可以将其替换为vector<int> p[MAXN];并按如下方式工作:

if (d[cur] + w_extra < d[next]){
    d[next] = d[cur] + w_extra;
    p[next].clear();
    p[next].push_back(cur);
    q.push(dist_node(d[next], next));
}
else if(d[cur] + w_extra == d[next]){
    p[next].push_back(cur); // a new shortest way of hitting this same node
}

你还需要更新你的findpath()函数,因为它需要处理导致多条路径的"分支",可能是指数级的大量路径,这取决于图。如果您只需要打印路径,您可以这样做:

int answer[MAXN];
void findpath (int t, int depth){
    if(t == -1){ // we reached the initial node of one shortest path
        for(int i = depth-1; i >= 0; --i){
            printf("%d ", answer[i]);
        }
        printf("%dn", last_node); // the target end node of the search
        return;
    }
    for(int i = p[t].size()-1; i >= 0; --i){
        answer[depth] = p[t][i];
        findpath(p[t][i], depth+1);
    }
}

注意,除了在大小写之间清除向量数组之外,您还需要在dijkstra的开头执行p[s].push_back(-1)操作。