【问题标题】:calculate next shortest path(minimum hop to destination)计算下一条最短路径(到目的地的最小跳数)
【发布时间】:2014-04-28 06:32:55
【问题描述】:

这是 omnet 使用 Dijkstra 计算最短路径(跳)的结构。

void cTopology::calculateUnweightedSingleShortestPathsTo(Node * _target) {
    // multiple paths not supported :-(
    if (!_target) throw cRuntimeError(this, "..ShortestPathTo(): target node is NULL");
    target = _target;
    for (int i = 0; i < num_nodes; i++) {
        nodev[i].known = false; // not really needed for unweighted
        nodev[i].dist = INFINITY;
        nodev[i].out_path = NULL;
    }
    target - > dist = 0;
    std::deque < Node * > q;
    q.push_back(target);
    while (!q.empty()) {
        Node * v = q.front();
        q.pop_front();
        // for each w adjacent to v...
        for (int i = 0; i < v - > num_in_links; i++) {
            if (!(v - > in_links[i] - > enabl)) continue;
            Node * w = v - > in_links[i] - > src_node;
            if (!w - > enabl) continue;
            if (w - > dist == INFINITY) {
                w - > dist = v - > dist + 1;
                w - > out_path = v - > in_links[i];
                q.push_back(w);
            }
        }
    }
}

我想找到并记录下一个最短的一跳。谁能帮我解决这个问题?理论上,我需要创建一个新结构来计算最短路径,而无需先前选择的下一个节点对吗?谢谢

【问题讨论】:

    标签: c++ dijkstra omnet++


    【解决方案1】:

    让它更容易..
    你可以用...

    父[]
    存储数据/ id'关于前一个节点..

    那么要获得路径,你只需要反转它..

    父母[父母[父母[目标]] -> 父母[父母[目标] -> 父母[目标]

    代码,你可以看这里...

    http://pastebin.com/YUBicFjy 

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2016-06-02
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多