1026. Maximum Cost of Trip With K Highways
Given n cities, labeled from 0 to n - 1, and a network of highways connecting them. The highways are represented as a 2D integer array highways, where each entry highways[i] = [city1i, city2i, tolli] denotes a bidirectional road between city1i and city2i with a toll cost of tolli.
Additionally, you are given an integer k, which specifies the exact number of highways you must travel.
You can begin your journey at any city, but you cannot visit the same city more than once.
Return the maximum possible toll cost for a valid trip of length k. If no such trip exists, return -1.
Example 1:
Input: n = 6, highways = [[0,2,5],[2,3,7],[1,3,8],[4,5,10],[3,4,6]], k = 3Â Â
Output: 24Â Â
Explanation:
One possible trip is: 1 -> 3 -> 4 -> 5, with a total cost of 8 + 6 + 10 = 24.
Another valid trip is: 2 -> 3 -> 4 -> 5, with the same total cost of 7 + 6 + 10 = 23.
It can be proven that 24 is the highest toll cost for a valid trip.
Example 2:
Input: n = 4, highways = [[0,1,4],[2,3,3]], k = 3Â Â
Output: -1Â Â
Explanation:
There are no valid trips of length 3, so return -1.
Now Your Turn!
Pick the correct output for the given inputInput: n = 5, highways = [[0,2,7],[2,3,4],[1,4,9],[3,4,5]], k = 2Â Â
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 ≤ n ≤ 15
- 1 ≤ highways.length ≤ 50
- highways[i].length == 3
- 0 ≤ city1i, city2i ≤ n - 1
- city1i ≠city2i
- 0 ≤ tolli ≤ 100
- 1 ≤ k ≤ 50
- No duplicate highways exist.