简体   繁体   English

Dijkstra 的最短路径算法实现出错(使用 C++ STL)

[英]Error in Dijkstra's shortest path algorithm implementation (using C++ STL)

I have tried to implement Dijkstra's algorithm in C++ using std::priority_queue.The function "dijsktra" will take input as 2 nodes, the source vertex and the destination vertex.我尝试使用 std::priority_queue 在 C++ 中实现 Dijkstra 的算法。function “dijsktra”将输入作为 2 个节点,源顶点和目标顶点。 However, I am getting incorrect answers every time.但是,我每次都得到不正确的答案。 Kindly help me out and tell me where I have erred.请帮帮我,告诉我哪里出错了。 My code-我的代码-

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#define inf 9999999
using namespace std;

map <int,int> vis;
vector <vector <pair <int,int> > > adj(100);
vector <int> dist(100);

void dijkstra(int u,int t)
{
    priority_queue <pair <int,int> > q;
    int b,w,i;
    q.push({0,u});
    vis[u]=1;
    
    while(!q.empty())
    {
        u = q.top().second; q.pop();
        
        if(!vis[u])
        {
            vis[u]=1;
            for(i=0;i<adj[u].size();i++)
            {
                b = adj[u][i].first; w = adj[u][i].second;
                if(dist[b]>dist[u]+w)
                {
                    dist[b] = dist[u] + w;
                    q.push({-dist[b],b});
                }
            }
        }
    }
    
    cout << dist[t];
}


int main()
{
    int i,j,k,n,m,x,y,w,t;
    
    cin >> n >> m;
    for(i=0;i<m;i++)
    {
        cin >> x >> y >> w;
        adj[x].push_back({y,w});
        adj[y].push_back({x,w});
    }
    
    cin >> t;
    
    for(i=1;i<=n;i++)
    {
        dist[i]=inf;
    }
    
    dijkstra(1,t);
}

Mistake:错误:

Your code is not working due to vis[u] = 1 which never lets you enter the for loop.由于vis[u] = 1永远不会让您进入 for 循环,您的代码无法正常工作。

Actually there is no need of having a visited array in Dijkstra algorithm (atleast for this case).实际上,在Dijkstra algorithm中不需要visited array (至少在这种情况下)。 But using visited array here will decrease the time complexity and when there are negative edges it will be tricky, so be careful there.但是在这里使用visited array会降低时间复杂度,当有负边缘时会很棘手,所以要小心。

#include <iostream>
#include <vector>
#include <queue>
#define inf 9999999

std::vector <std::vector <std::pair <int,int> > > adj(100);
std::vector <int> dist(100);

void dijkstra(int u,int t)
{
    std::priority_queue <std::pair<int, int>> q;
    int b, w, i;

    q.push({0, u});
    while(!q.empty()) {
        u = q.top().second; 
        q.pop();
        for(i = 0; i < adj[u].size(); i++) {
            b = adj[u][i].first; w = adj[u][i].second;
            if(dist[b] > dist[u] + w) {
                dist[b] = dist[u] + w;
                q.push({-dist[b], b});
            }
        }
    }
    
    std::cout << dist[t];
}


int main()
{
    int i, n, m, x, y, w, t;
    
    std::cin >> n >> m;
    for(i = 0;i < m; i++) {
        std::cin >> x >> y >> w;
        adj[x].push_back({y, w});
        adj[y].push_back({x, w});
    }
    
    std::cin >> t;
    
    for(i = 0; i < n; i++) {
        dist[i] = inf;
    }
     
    // making the source distance to 0
    dist[0] = 0;
    
    dijkstra(0,t);
}

The problem is vis[u]=1;问题是vis[u]=1; just after q.push({0,u});就在q.push({0,u});之后. .

Due to that, the check if(!vis[u]) cannot be passed and no nodes will be processed.因此,检查if(!vis[u])无法通过,并且不会处理任何节点。

You should do dist[u]=0;你应该做dist[u]=0; instead of that.而不是那个。

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

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