High Score
You are playing a game consisting of n rooms numbered from 1 to n. You are given a 2D integer array tunnels where tunnels[i] = [u, v, score] represents a directed tunnel starting at room u, ending at room v, and increasing your score by score (which can be positive, negative, or zero). You may travel through the same tunnel multiple times.
Your task is to find the maximum possible score you can achieve by traveling from room 1 to room n. If you can get an arbitrarily large score (because of an infinitely repeating positive cycle on a valid path), return -1.
You can assume it is always possible to reach room n from room 1.
Example 1:
Input: n = 4, tunnels = [[1, 2, 3], [2, 4, -1], [1, 3, -2], [3, 4, 7], [1, 4, 4]]
Output: 5
Explanation: * Path 1 -> 2 -> 4 gives score: 3 + (-1) = 2.
- Path 1 -> 4 gives score: 4.
- Path 1 -> 3 -> 4 gives score: (-2) + 7 = 5.
- The maximum score is 5.
Example 2:
Input: n = 3, tunnels = [[1, 2, 10], [2, 1, 10], [1, 3, 5]]
Output: -1
Explanation:
You can bounce between room 1 and room 2 infinitely, gaining 20 score each round trip, before finally going to room 3. The score can be arbitrarily large.
Now Your Turn!
Pick the correct output for the given inputInput :n = 4, tunnels = [[1, 2, 5], [2, 3, 10], [3, 2, 10], [1, 4, 0]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 2500
- 1 <= tunnels.length <= 5000
- tunnels[i].length == 3
- 1 <= tunnels[i][0], tunnels[i][1] <= n
- -109 <= tunnels[i][2] <= 109