2dbi
Home/Ola/Shortest Path Between Cities with Tolls (Dijkstra)
OOla·DSASDE-1Online Assessment

Shortest Path Between Cities with Tolls (Dijkstra)

Problem

You are given a road network as a weighted directed graph where edge weights represent toll costs. Find the minimum cost path between a source city and a destination city.

Example

cities = 5
roads = [[0,1,4],[0,2,2],[1,3,3],[2,1,1],[2,3,5],[3,4,2]]
source = 0, destination = 4
Output: 8  -- path: 0→2→1→3→4

Constraints

  • 1 ≤ cities ≤ 10^4
  • Non-negative edge weights

Follow-up variants asked at Ola

  • Find shortest path with at most k stops
  • Find shortest path avoiding certain toll booths
  • Multiple sources to a single destination (reverse Dijkstra)
added 6 days ago
LeadersAccount