Code Logo

Find Cheapest Delivery Route

Published at19 Apr 2026
Hard 0 views
Like0

This problem is based on a delivery app kind of situation. You have several locations connected by roads, and each road has a travel cost. The job is to figure out the cheapest total cost to get from the starting point to the destination.

The tricky part is that the direct road is not always the best one. Sometimes a route with more stops ends up cheaper overall, so you need to compare total cost, not just the next move.

For example, if you can go from 1 to 2 with cost 5, from 1 to 3 with cost 2, and from 3 to 2 with cost 1, then reaching 2 through 3 costs only 3. That is cheaper than taking the direct road.

So the task is to find the minimum travel cost from the start location to the target location. If there is no route that reaches the target, return -1.

Example Input & Output

Example 1
Input
n = 4, roads = [[1,2,5],[1,3,2],[3,2,1],[2,4,2],[3,4,7]], start = 1, target = 4
Output
5
Explanation

The cheapest route is 1 -> 3 -> 2 -> 4 with total cost 5.

Example 2
Input
n = 5, roads = [[1,2,10],[1,3,3],[3,2,1],[2,5,2],[3,4,8],[4,5,1]], start = 1, target = 5
Output
6
Explanation

The cheapest path is 1 -> 3 -> 2 -> 5 with total cost 6.

Example 3
Input
n = 3, roads = [[1,2,4]], start = 1, target = 3
Output
-1
Explanation

There is no route that reaches location 3.

Algorithm Flow

Recommendation Algorithm Flow for Find Cheapest Delivery Route
Recommendation Algorithm Flow for Find Cheapest Delivery Route

Solution Approach

A good method for this problem is to keep track of the best cost you know so far for each location, and keep improving those costs as you discover better routes.

The reason this works well is that you do not need to guess the full route all at once. You can build the answer piece by piece by always expanding the location that currently has the smallest known total cost.

We start by storing a very large value for every location, because at the beginning we do not know any route yet. Then we set the starting location cost to 0, since it costs nothing to begin there.

The update rule looks like this:

new_cost = dist[current] + road_cost

If new_cost is smaller than the cost we had saved for the next location, we replace the old value. That means we found a cheaper way to get there.

The main loop keeps choosing the unvisited location with the smallest known cost. From there, it checks all outgoing roads and updates the neighboring locations when a cheaper route appears.

If we eventually reach the target location, the saved value for that location is the cheapest possible total cost. If the target still has the large default value after processing, then it was never reachable, so the answer is -1.

So the full idea is: start from the source, keep the cheapest known cost for every location, and repeatedly improve those costs until the destination is settled or no reachable locations remain.

Best Answers

Solutions Coming Soon

Verified best solutions for this Challenge are still being analyzed and will be available soon.