Objective
In a graph with weights the path with fewer edges is not necessarily the cheapest. Dijkstra’s algorithm uses a priority queue to always visit the node with the smallest known distance (weights are not negative).
Instructions
- The first line has
n m(nodes, from 0 to n-1, and edges); thenmlinesu v w(undirected edge with weightw); and finally a linestart end. - Show
Distance: dwith the smallest sum of weights, orNo pathif there is no path.
4 4
0 1 1
1 2 2
0 2 5
2 3 1
0 3
Distance: 4
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise