Choose a Road to Maximize Average Route Toll Increase
Problem statement
A directed acyclic graph has cities 0 through n-1. Each road is [from,to,toll]. Every route means a directed path from city 0 to city n-1.
Increase exactly one road's toll by one. Return the zero-based input index that maximizes the average toll increase across all routes. Roads on no complete route contribute zero; break ties by smaller input index. Return -1 when no route exists.
Function
bestRoadToIncrease(n: int, roads: int[][]) → intExamples
Example 1
n = 4roads = [[0,1,5],[0,2,1],[1,3,2],[2,3,9]]return = 0Every road belongs to one of two routes, so index zero wins the tie.
Example 2
n = 4roads = [[0,1,1],[1,2,1],[1,3,1],[2,3,1]]return = 0Road 0 appears on both complete routes.
Example 3
n = 3roads = [[0,1,2]]return = -1No route reaches the destination.
Constraints
2 <= n <= 100000.- The graph is a DAG.
- Path counts fit in a signed 64-bit integer.