Minimum Cost to Reach Destination in Time - Problem
Imagine you're planning a road trip across a country with n cities numbered from 0 to n-1. All cities are connected by bi-directional roads, but here's the catch: every city charges a passing fee just to enter!
You start at city 0 and need to reach city n-1 within maxTime minutes. Your challenge is to find the minimum cost route that gets you there on time.
Input:
edges[i] = [xi, yi, timei]- A road between cities xi and yi taking timei minutespassingFees[j]- The fee you pay when entering city jmaxTime- Maximum time allowed for your journey
Output: Return the minimum cost to reach the destination within the time limit, or -1 if impossible.
Note: You pay the passing fee for every city you visit, including your starting city and destination!
Input & Output
example_1.py โ Basic Path Selection
$
Input:
maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โบ
Output:
11
๐ก Note:
The optimal path is 0 โ 1 โ 2 โ 5. Total time: 10+10+10 = 30 minutes (exactly within limit). Total cost: 5+1+2+3 = 11 dollars.
example_2.py โ Time Constraint Forces Expensive Route
$
Input:
maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โบ
Output:
48
๐ก Note:
With one less minute, the direct path 0โ1โ2โ5 takes 30 minutes (too long). Must take 0โ3โ4โ5: time = 1+10+15 = 26 minutes, cost = 5+20+20+3 = 48 dollars.
example_3.py โ Impossible Within Time Limit
$
Input:
maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
โบ
Output:
-1
๐ก Note:
No path can reach the destination within 25 minutes. The fastest path takes 26 minutes (0โ3โ4โ5).
Visualization
Tap to expand
Understanding the Visualization
1
Start Your Journey
You begin at city 0, paying the entrance fee immediately. Your GPS (Dijkstra's algorithm) starts calculating optimal routes.
2
Explore Route Options
At each city, consider all connected roads. Can you afford the time for this route? Is this the cheapest way to reach the next city by this time?
3
Update Your Route Map
Keep track of the cheapest way to reach each city at each possible time. If you find a better route, update your plan.
4
Reach Destination
The first time you reach the destination city, you've found the optimal route (thanks to processing minimum costs first).
Key Takeaway
๐ฏ Key Insight: By tracking states as (city, time_used) pairs and always processing minimum cost first, we efficiently find the cheapest route that respects our time deadline!
Time & Space Complexity
Time Complexity
O(n!)
In worst case, we might explore all permutations of cities
โ Quadratic Growth
Space Complexity
O(n)
Recursion stack depth up to number of cities
โก Linearithmic Space
Constraints
- 1 โค n โค 1000
- n - 1 โค edges.length โค 1000
- 0 โค xi, yi โค n - 1
- 1 โค timei โค 1000
- 1 โค passingFees[j] โค 1000
- 0 โค maxTime โค 1000
- The graph is connected (there exists a path from city 0 to city n-1)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code