Cheapest route when driving against a one-way road costs that road's toll
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
This is the follow-up to the minimum road reversal problem: driving against a road now costs that road's own toll instead of a flat count of one.
A city has `n` intersections labeled `0` to `n - 1`, connected by one-way roads. Each road is given as `[u, v, toll]`. Driving it legally from `u` to `v` is free. Driving it against its direction, from `v` to `u`, is allowed and costs `toll`.
Given the road list, a `source` intersection and a `target` intersection, return the minimum total toll needed to drive from `source` to `target`. If `source == target`, return `0`. If `target` cannot be reached from `source` even when roads can be driven against their direction, return `-1`.
### Function Signature
```python
def min_reversal_toll(n: int, roads: list[list[int]], source: int, target: int) -> int:
```
### Rules
- Driving road `[u, v, toll]` from `u` to `v` costs 0. Driving it from `v` to `u` costs `toll`.
- Every traversal against a road's direction is charged, including repeated traversals of the same road.
- Tolls are non-negative integers. A toll of 0 makes driving that road backward free.
- Several roads may connect the same pair of intersections, with equal or different tolls; each one is a separate road.
- A route with more reversals can cost less than a route with fewer reversals. The result is the minimum total toll over all routes, returned as a single integer.
### Constraints
- `1 <= n <= 10^5`
- `0 <= len(roads) <= 2 * 10^5`
- For every road `[u, v, toll]`: `0 <= u, v < n`, `u != v` and `0 <= toll <= 10^9`
- `0 <= source, target < n`
- The answer can reach roughly `(n - 1) * 10^9`, about `10^14`. That exceeds `2^31 - 1` but stays below `2^53`, so use 64-bit integers in typed languages.
### Examples
#### Example 1
```text
Input: n = 5, roads = [[0,1,5],[2,1,3],[2,3,4],[4,3,7]], source = 0, target = 4
Output: 10
```
The route 0 → 1 → 2 → 3 → 4 drives `[0,1,5]` legally (0), `[2,1,3]` backward (3), `[2,3,4]` legally (0) and `[4,3,7]` backward (7), for a total of 10. Every route into intersection 4 must drive `[4,3,7]` backward, and the only way out of intersections 0 and 1 is driving `[2,1,3]` backward, so 10 is the minimum.
#### Example 2
```text
Input: n = 4, roads = [[3,0,10],[1,0,2],[2,1,3],[2,3,6]], source = 0, target = 3
Output: 5
```
Driving `[3,0,10]` backward reaches intersection 3 with one reversal for a toll of 10. The route 0 → 1 → 2 → 3 drives `[1,0,2]` backward (2), `[2,1,3]` backward (3) and `[2,3,6]` legally (0). It uses two reversals and costs 5, which is the minimum.
#### Example 3
```text
Input: n = 3, roads = [[0,1,4]], source = 0, target = 2
Output: -1
```
No road touches intersection 2.
Overview: A follow-up graph problem from a Google technical screen: each one-way road has its own toll for driving against its direction, and you must return the cheapest total toll from a source intersection to a target. It tests shortest-path reasoning on non-negative weighted edges, 64-bit cost handling, and unreachable cases.
A city has `n` intersections labeled `0` to `n - 1`, connected by one-way roads. Each road is given as `[u, v, toll]`. Driving it legally from `u` to `v` is free. Driving it against its direction, from `v` to `u`, is allowed and costs `toll`.
Given `n`, the road list `roads`, a `source` intersection and a `target` intersection, return the minimum total toll needed to drive from `source` to `target`. If `source == target`, return `0`. If `target` cannot be reached from `source` even when roads can be driven against their direction, return `-1`.
### Rules
- Driving road `[u, v, toll]` from `u` to `v` costs 0. Driving it from `v` to `u` costs `toll`.
- Every traversal against a road's direction is charged, including repeated traversals of the same road.
- Tolls are non-negative integers. A toll of 0 makes driving that road backward free.
- Several roads may connect the same pair of intersections, with equal or different tolls; each one is a separate road.
- A route with more reversals can cost less than a route with fewer reversals. The result is the minimum total toll over all routes, returned as a single integer.
### Example 1
```text
Input: n = 5, roads = [[0,1,5],[2,1,3],[2,3,4],[4,3,7]], source = 0, target = 4
Output: 10
```
The route 0 -> 1 -> 2 -> 3 -> 4 drives `[0,1,5]` legally (0), `[2,1,3]` backward (3), `[2,3,4]` legally (0) and `[4,3,7]` backward (7), for a total of 10. Every route into intersection 4 must drive `[4,3,7]` backward, and the only way out of intersections 0 and 1 is driving `[2,1,3]` backward, so 10 is the minimum.
### Example 2
```text
Input: n = 4, roads = [[3,0,10],[1,0,2],[2,1,3],[2,3,6]], source = 0, target = 3
Output: 5
```
Driving `[3,0,10]` backward reaches intersection 3 with one reversal for a toll of 10. The route 0 -> 1 -> 2 -> 3 drives `[1,0,2]` backward (2), `[2,1,3]` backward (3) and `[2,3,6]` legally (0). It uses two reversals and costs 5, which is the minimum.
### Constraints
- `1 <= n <= 10^5`
- `0 <= len(roads) <= 2 * 10^5`
- For every road `[u, v, toll]`: `0 <= u, v < n`, `u != v` and `0 <= toll <= 10^9`
- `0 <= source, target < n`
- The answer can reach roughly `(n - 1) * 10^9`, about `10^14`. That exceeds `2^31 - 1` but stays below `2^53`, so return a 64-bit integer: `long` in Java and `long long` in C++.
Constraints
- 1 <= n <= 10^5
- 0 <= len(roads) <= 2 * 10^5
- For every road [u, v, toll]: 0 <= u, v < n, u != v and 0 <= toll <= 10^9
- 0 <= source, target < n
- The answer can reach roughly (n - 1) * 10^9, about 10^14, which exceeds 2^31 - 1 and stays below 2^53; Java returns long and C++ returns long long.
Examples
Input: (1, [], 0, 0)
Expected Output: 0
Explanation: Single intersection with no roads; source equals target, so the toll is 0.
Input: (4, [[0, 1, 5], [1, 2, 3], [2, 0, 7], [3, 2, 1]], 2, 2)
Expected Output: 0
Explanation: Source equals target in a graph that has roads, so the answer is 0 without driving.
Hints
- Each road offers two moves with different prices. Write down both moves for every road, and keep parallel roads as separate moves.
- In Example 2 a route with two reversals costs less than a route with one, so the quantity to minimize is the total toll paid along the route.
- Every toll is non-negative, and a sum of many tolls can exceed 2^31 - 1.