1

我正在使用以下代码来计算从 G 中的源节点 s 到所有其他节点的最短路径长度:

void class::initilize(int T, ListDigraph &G, ListDigraph::ArcMap <int> &duration,
                                  ListDigraph::ArcMap <int> &capacity, ListDigraph::NodeMap <int> &supply) {
vector<int> to_dist(countNodes(G));
vector<int> from_dist(countNodes(G));
Node s = getsource();
Node t = getsink();
//ListDigraph::NodeMap<int> dist(G);
lemon::Dijkstra<ListDigraph, Duration> dijkstra_instance(G, duration);
dijkstra_instance.run(s);
for (ListDigraph::NodeIt n(G); n != INVALID; ++n) {
    for (Node v=n;v != s; v=dijkstra_instance.predNode(v)) {
        std::cout << G.id(v) << "<-";
    }
    cout << dijkstra_instance.dist(n) << endl;
    cout << G.id(n) << endl;
    to_dist[G.id(n)] = dijkstra_instance.dist(n);
    dijkstra_instance.run(n, t);
    from_dist[G.id(n)] = dijkstra_instance.dist(t);
}

但是,我将计算的路径长度与真实路径长度进行比较,它们不是真的。即使路径错误,输出中也会出现节点 ID -1 的节点。当我现在将 dijkstra_instance.run(s) 放入 for 循环时,计算是正确的。我是否使用了 dijkstra 命令错误?

4

0 回答 0