Dijkstra’s shortest paths

Find the cost of the cheapest route from A to B along the listed directed edges.

Examples

Example 1

Input
A→B: 7, A→C: 2, C→B: 1
Expected output
3

Example 2

Input
A→B: 2, A→C: 3, C→B: 2
Expected output
2

Hints and solution

Hint 1

The direct route might be more expensive than a route through C.

Reveal worked answer

The first graph offers cost 7 directly or cost 2 + 1 through C, for a minimum of 3. In the second graph the direct cost 2 is cheaper than 3 + 2.

Back to the lesson