Advanced graphs and trees · 9 / 15 Exercise Hard

Weighted shortest path (Dijkstra)

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); then m lines u v w (undirected edge with weight w); and finally a line start end.
  • Show Distance: d with the smallest sum of weights, or No path if there is no path.
Input
4 4
0 1 1
1 2 2
0 2 5
2 3 1
0 3
Output
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