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
Visualization
Time & Space Complexity
For each of 2^n possible masks, n cities, and k steps, we check n neighbors
DP table stores max cost for each (mask, city) combination
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