Maximum Cost of Trip With K Highways - Problem

Imagine you're planning the ultimate road trip adventure across a network of cities connected by toll highways! ๐Ÿš—๐Ÿ’ฐ

You have n cities numbered from 0 to n-1, connected by a series of bidirectional highways. Each highway has a toll cost, and here's the twist: you want to maximize your spending to get the most luxurious trip possible!

Given a 2D array highways where highways[i] = [city1, city2, toll] represents a highway connecting two cities with a specific toll cost, your goal is to plan a trip that:

  • Uses exactly k highways
  • Visits each city at most once
  • Maximizes the total toll cost
  • Can start from any city

Return the maximum possible cost for such a trip, or -1 if no valid trip exists.

Input & Output

example_1.py โ€” Basic Highway Network
$ Input: n = 4, highways = [[0,1,3],[2,1,5],[0,2,7]], k = 2
โ€บ Output: 10
๐Ÿ’ก Note: The optimal trip is 0 โ†’ 2 โ†’ 1 using highways with costs 7 + 5 = 12. Wait, let me recalculate: 0 โ†’ 2 (cost 7) โ†’ 1 (cost 5) gives total cost 12, but we can also do 2 โ†’ 0 (cost 7) โ†’ 1 (cost 3) giving cost 10. Actually, the maximum is 0 โ†’ 2 โ†’ 1 with cost 7 + 5 = 12.
example_2.py โ€” No Valid Path
$ Input: n = 4, highways = [[0,1,3],[1,2,5]], k = 3
โ€บ Output: -1
๐Ÿ’ก Note: There are only 2 highways available, but we need exactly 3 highways for our trip. Since k = 3 > number of available highways, return -1.
example_3.py โ€” Single Highway
$ Input: n = 3, highways = [[0,1,2],[1,2,3],[0,2,4]], k = 1
โ€บ Output: 4
๐Ÿ’ก Note: We need exactly 1 highway. The maximum cost single highway is from city 0 to city 2 with cost 4.

Visualization

Tap to expand
0123$3$7$5$4Optimal Path (k=2)Highway Network๐ŸŽฏ Goal: Find path using exactly k=2 highways๐Ÿ’ฐ Maximize total toll cost๐Ÿš— Visit each city at most onceBitmask DP001: City 0 visited011: Cities 0,1 visited101: Cities 0,2 visiteddp[mask][city] = max cost
Understanding the Visualization
1
Build Graph
Create adjacency list representation of the highway network
2
Initialize DP
Set starting states: dp[1<<i][i] = 0 for each city i
3
Explore States
For each state, try extending to unvisited neighboring cities
4
Find Maximum
Check all states with exactly k+1 cities visited (k highways used)
Key Takeaway
๐ŸŽฏ Key Insight: Bitmask DP efficiently tracks visited cities while building optimal paths, avoiding the exponential complexity of naive backtracking by storing intermediate results.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n * n^2 * k)

For each of 2^n possible masks, n cities, and k steps, we check n neighbors

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n * n)

DP table stores max cost for each (mask, city) combination

n
2n
โš  Quadratic Space

Constraints

  • 2 โ‰ค n โ‰ค 15
  • 0 โ‰ค highways.length โ‰ค 50
  • highways[i].length == 3
  • 0 โ‰ค city1i, city2i โ‰ค n - 1
  • city1i != city2i
  • 0 โ‰ค tolli โ‰ค 100
  • 1 โ‰ค k โ‰ค 50
  • Each pair of cities has at most one highway connecting them
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
22.2K Views
Medium Frequency
~25 min Avg. Time
847 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