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).
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)
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code