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 minutes
  • passingFees[j] - The fee you pay when entering city j
  • maxTime - 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
Minimum Cost to Reach Destination in Time INPUT City Graph (6 cities) 0 $5 1 $1 2 $2 3 $20 4 $20 5 $3 10m 10m 10m 1m 10m 15m Input Values: 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] ALGORITHM (DP) 1 Initialize DP Table dp[time][city] = min cost dp[0][0] = 5 (start fee) 2 Iterate Time 0..maxTime For each time t, check all reachable cities 3 Update Neighbors For edge (u,v,time): dp[t+time][v] = min( dp[t][u] + fees[v]) 4 Find Minimum Return min(dp[t][n-1]) for all t <= maxTime Key DP States: t=0: dp[0][0]=5 t=10: dp[10][1]=6 t=20: dp[20][2]=8 t=30: dp[30][5]=11 (OK!) Path: 0-->1-->2-->5 FINAL RESULT Optimal Path Found 0 $5 10m 1 $1 10m 2 $2 10m 5 $3 Cost Breakdown: 5 + 1 + 2 + 3 = 11 Time: 10+10+10 = 30 <= 30 Output: 11 Key Insight: Use 2D DP where dp[time][city] tracks minimum cost to reach each city at each time. Unlike shortest path, we need to track time dimension because a longer path might have lower cost. The optimal path 0-->1-->2-->5 costs $11 and takes exactly 30 minutes (within limit). TutorialsPoint - Minimum Cost to Reach Destination in Time | Dynamic Programming Approach
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen