简体   繁体   English

dijkstra算法优先级队列

[英]dijkstra algorithm priority queue

When we are writing dijkstra algorithm with priority queue why we are not check about the visited node?当我们使用优先队列编写 dijkstra 算法时,为什么我们不检查访问节点?

while (!pq.empty())
    {
        
        int u = pq.top().second;
        pq.pop();
 
        // Get all adjacent of u.
        for (auto x : adj[u])
        {
            int v = x.first;
            int weight = x.second;
 
            if (dist[v] > dist[u] + weight)
            {
                dist[v] = dist[u] + weight;
                pq.push(make_pair(dist[v], v));
            }
        }

It does check the previous value of the node in dist[v] , which I assume stores the current best distance from the root (or u ) to node v .它确实检查了dist[v]中节点的先前值,我假设它存储了从根(或u )到节点v的当前最佳距离。 If a new path is found to v which is shorter than the previous shortest one, it is reinserted into the priority queue because it may now provide shorter paths to other nodes.如果找到一条到v的新路径比之前的最短路径短,则将其重新插入优先级队列,因为它现在可能提供到其他节点的更短路径。 If this new distance to v is longer than the previous, then it is left alone.如果这个到v的新距离比之前的长,那么它就不管了。 This is why there is no else in the implementation.这就是为什么在实现中没有else的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM