Ride Detour Fareboard
Lowest route costs with repeated vertices and tied costs retained
A ride-sharing app is testing an itinerary fareboard on a directed road network. Intersections are numbered from 0 to n - 1. Each entry roads[i] = [u, v, fare] describes a road from u to v that adds fare to an itinerary's cost.
An itinerary starts at intersection 0 and ends the first time it reaches intersection n - 1. It may revisit other intersections and traverse the same road multiple times. Its total cost is the sum of the fares of all road traversals.
Road entries are distinct choices, even when two entries have identical endpoints and fares. Two itineraries are different if their sequences of road-entry indices differ. Different itineraries with equal total costs occupy separate places on the fareboard.
Return the costs of the k cheapest itineraries in nondecreasing order, retaining repeated costs. If fewer than k itineraries exist, return all their costs. If none exist, return an empty list.
When n = 1, the start is already the destination: the empty itinerary, with cost zero, is the only itinerary.
Examples
Example 1
Input: n = 3, roads = [[0,2,8],[0,1,2],[1,1,3],[1,2,1]], k = 4 Output: [3,6,8,9]
The app can take the direct road or go through intersection 1. Before leaving intersection 1 for the destination, an itinerary may traverse its self-loop any number of times. These are separate itineraries whose costs compete with the direct road.
Example 2
Input: n = 4, roads = [[0,1,3],[1,3,4],[0,2,1],[2,3,8]], k = 5 Output: [7,9]
There are only two itineraries: one through intersection 1 and one through intersection 2. Since the requested number exceeds the number available, both costs are returned.
Example 3
Input: n = 2, roads = [[0,1,4],[0,1,4]], k = 3 Output: [4,4]
The two identical road entries represent different choices. Each forms a different itinerary, so their equal costs both appear.
Constraints
- 1 <= n <= 2000
- 0 <= roads.length <= 3000
- roads[i] has exactly three integers [u, v, fare]
- 0 <= u, v < n
- 1 <= fare <= 1000000
- 1 <= k <= 50
- Self-loops and parallel road entries are allowed
- Every returned cost fits in a signed 32-bit integer
Use a min-heap of partial itineraries and expand each intersection at most k times. Do not expand an arrival at the destination. Equal-cost heap entries must remain separate. The reference algorithm takes O(n + m + km log(2 + km)) time and O(n + m + km) space, where m is the number of road entries.
Hints
Show hint 1Hint 1
A single cheapest arrival at an intersection is not enough: more expensive arrivals can still lead to itineraries that belong on the fareboard.
Show hint 2Hint 2
Process partial itineraries by increasing cost. Once k arrivals at an intersection have been processed, consider why any later arrival cannot be needed for the first k completed itineraries.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you change the algorithm if equal total costs should appear only once, regardless of how many itineraries attain them?
- How would you return the actual road-entry sequences as well as their costs?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem