Run time error?
Hey, pick up a case and try to dry run your code on it, and try to find out why you’re getting wrong answer, if still not solved, then tell.
please help me out could’t able to find the problem
@raj.verma5454
For unreachable nodes, you are required to print -1. Consider a testcase like
1
4 2
1 2 24
1 4 20
1
The expected output is
24 -1 20
However you code prints INT_MAX in place of -1.
@raj.verma5454
Testcase :
1
4 2
1 2 24
1 4 20
4
Expected Output :
20 44 -1
Your Output :
-1 -1 -1
Explanation :
Your implementation of Dijkstra Algo is wrong. In Dijkstra algorithm , you pick the node with the least distance and then work with its neighbour and proceed this way till all nodes are visited.
In your code, though you do mark the distance of source node as 0, your loop begins with 1 and goes till n ( I’m talking about the loop with i variable).
You visit each node in increasing order of their node values. The graph may not always be connected that way as in the testcase above. It may even be not connected at all. Implementing a simple loop as such will not work. Use a sorting data structure like priority queue or a set and work with that. This is a greedy algorithm. Make the according changes in your code.
I checked , for input 1 4 2 1 2 24 1 4 20 4 , it is giving
it is giving right output 20 44 -1
And I must say please resolve it soon , it’s been 3 days my doubt is still pending
Try changing your implementation this way.
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.