Dijkstra algorithm


can you tell me the mistake plz.

Got some errors in your code:

  1. in line :
    map<int,list<int,int>> m;
    it should be map<int,list<pair<int,int> >> m;

  2.         dist[i]=INT_MAX;
    

The above line is wrong
acc to your code, it should be dist[i.first] = INT_MAX;

  1. for(auto d : dist)
    {
    cout<<d.second<<" ";
    }
    cout<<endl;

the above printing of ans has to be done outside the while loop
while(!s.empty())

Also you don’t need to print distance of source from source(viz 0). It’s given in the question that you need to print n-1 integers


made the required changes and got the correct ans for sample test case but the test case isnt getting passed .
Help plz

hey @Dhruv-Miglani-2090236747707980
Sorry for the late reply

for unreachable nodes, your code displays distance as infinite, while it’s mentioned in the question that you need to display -1 in such cases. Try writing

if(d.first!=src){
if(d.second != INT_MAX)
cout<<d.second<<" ";
else cout << -1 << " ";
}

while displaying your answers in the code, hope that works.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.