Quick 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.

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

  1. Each road offers two moves with different prices. Write down both moves for every road, and keep parallel roads as separate moves.
  2. 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.
  3. Every toll is non-negative, and a sum of many tolls can exceed 2^31 - 1.

Minimum Total Toll with Paid Road Reversals

hardGoogleSoftware Engineer

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

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

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++.

Examples

Example 1
Input
(1, [], 0, 0)
Output
0
Notes
Single intersection with no roads; source equals target, so the toll is 0.
Example 2
Input
(4, [[0, 1, 5], [1, 2, 3], [2, 0, 7], [3, 2, 1]], 2, 2)
Output
0
Notes
Source equals target in a graph that has roads, so the answer is 0 without driving.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_reversal_toll(n: int, roads: list[list[int]], source: int, target: int) -> int:
Sample call
min_reversal_toll(1, [], 0, 0)
Language
Case 1
Input
(1, [], 0, 0)
Expected
0
Case 2
Input
(4, [[0, 1, 5], [1, 2, 3], [2, 0, 7], [3, 2, 1]], 2, 2)
Expected
0

Browse More Questions

Show the approach

Approach

Turn every road [u, v, toll] into two directed moves: u -> v with cost 0 and v -> u with cost toll. A drive through the city is then a walk in this directed graph whose total toll is the sum of its move costs, so the answer is the cheapest walk from source to target. Parallel roads become separate moves, and the cheapest one is picked automatically.

All move costs are non-negative, so Dijkstra's algorithm applies. Keep dist[x], the cheapest known cost to reach intersection x, and a min-heap of (cost, intersection) entries. Pop the smallest entry, skip it when its cost is larger than dist[x] (a stale entry), and otherwise relax every move out of x. Invariant: when an intersection is popped with a cost equal to dist[x], that cost is final, because every other heap entry is at least as large and adding non-negative tolls cannot make a later path cheaper. The first time target is popped, its cost is the answer. With non-negative costs an optimal walk never needs to revisit an intersection, so charging repeated reversals does not change the minimum.

Edge cases: source == target returns 0 before any search; if the heap empties without reaching target, it is unreachable even with reversals and the function returns -1; zero tolls are ordinary zero-cost moves; totals are kept in 64-bit integers because they can reach about 10^14.

Time complexity:
O((n + m) log (n + m)), where m = len(roads)
Space complexity:
O(n + m)