Minimize Direction Violations in a Directed Road Network
There are n nodes numbered from 0 through n - 1. Every input edge [u, v] has an original direction from u to v, but you may physically traverse it in either direction.
-
Traversing an edge in its original direction costs
0
.
-
Traversing it against its original direction costs
1
.
Given start and end, return the minimum total cost of any route from start to end. Return -1 if the two nodes are disconnected even when edges may be traversed both ways.
Function Signature
def minimum_direction_violations(
n: int,
edges: list[list[int]],
start: int,
end: int,
) -> int:
...
Constraints
-
1 <= n <= 200_000
-
0 <= len(edges) <= 300_000
-
0 <= u, v < n
and
u != v
-
Multiple directed edges between the same pair of nodes are allowed.
-
0 <= start, end < n
Examples
Input: n = 5, edges = [[0, 1], [2, 1], [2, 3], [4, 3]], start = 0, end = 4
Output: 2
Input: n = 4, edges = [[0, 1], [1, 2]], start = 0, end = 3
Output: -1
Input: n = 3, edges = [[0, 1], [1, 2]], start = 2, end = 0
Output: 2