|
| 1 | +# 1129. Shortest Path with Alternating Colors |
| 2 | +You are given an integer `n`, the number of nodes in a directed graph where the nodes are labeled from `0` to `n - 1`. Each edge is red or blue in this graph, and there could be self-edges and parallel edges. |
| 3 | + |
| 4 | +You are given two arrays `redEdges` and `blueEdges` where: |
| 5 | + |
| 6 | +* <code>redEdges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is a directed red edge from node <code>a<sub>i</sub></code> to node <code>b<sub>i</sub></code> in the graph, and |
| 7 | +* <code>blueEdges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates that there is a directed blue edge from node <code>u<sub>j</sub></code> to node <code>v<sub>j</sub></code> in the graph. |
| 8 | + |
| 9 | +Return an array `answer` of length `n`, where each `answer[x]` is the length of the shortest path from node `0` to node `x` such that the edge colors alternate along the path, or `-1` if such a path does not exist. |
| 10 | + |
| 11 | +#### Example 1: |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> n = 3, redEdges = [[0,1],[1,2]], blueEdges = [] |
| 14 | +<strong>Output:</strong> [0,1,-1] |
| 15 | +</pre> |
| 16 | + |
| 17 | +#### Example 2: |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> n = 3, redEdges = [[0,1]], blueEdges = [[2,1]] |
| 20 | +<strong>Output:</strong> [0,1,-1] |
| 21 | +</pre> |
| 22 | + |
| 23 | +#### Constraints: |
| 24 | +* `1 <= n <= 100` |
| 25 | +* `0 <= redEdges.length, blueEdges.length <= 400` |
| 26 | +* `redEdges[i].length == blueEdges[j].length == 2` |
| 27 | +* <code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> < n</code> |
| 28 | + |
| 29 | +## Solutions (Rust) |
| 30 | + |
| 31 | +### 1. Solution |
| 32 | +```Rust |
| 33 | +use std::cmp::Reverse; |
| 34 | +use std::collections::BinaryHeap; |
| 35 | +use std::collections::HashSet; |
| 36 | + |
| 37 | +impl Solution { |
| 38 | + pub fn shortest_alternating_paths( |
| 39 | + n: i32, |
| 40 | + red_edges: Vec<Vec<i32>>, |
| 41 | + blue_edges: Vec<Vec<i32>>, |
| 42 | + ) -> Vec<i32> { |
| 43 | + let n = n as usize; |
| 44 | + let mut red_nexts = vec![vec![]; n]; |
| 45 | + let mut blue_nexts = vec![vec![]; n]; |
| 46 | + let mut nodes_heap = BinaryHeap::from([(Reverse(0), 0, true), (Reverse(0), 0, false)]); |
| 47 | + let mut visited = HashSet::new(); |
| 48 | + let mut answer = vec![(i32::MAX, i32::MAX); n]; |
| 49 | + answer[0] = (0, 0); |
| 50 | + |
| 51 | + for edge in &red_edges { |
| 52 | + red_nexts[edge[0] as usize].push(edge[1] as usize); |
| 53 | + } |
| 54 | + for edge in &blue_edges { |
| 55 | + blue_nexts[edge[0] as usize].push(edge[1] as usize); |
| 56 | + } |
| 57 | + |
| 58 | + while let Some((Reverse(length), node, is_red)) = nodes_heap.pop() { |
| 59 | + if visited.contains(&(node, is_red)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + visited.insert((node, is_red)); |
| 64 | + |
| 65 | + if is_red { |
| 66 | + for &next in &blue_nexts[node] { |
| 67 | + if answer[next].1 > length + 1 { |
| 68 | + answer[next].1 = length + 1; |
| 69 | + nodes_heap.push((Reverse(length + 1), next, false)); |
| 70 | + } |
| 71 | + } |
| 72 | + } else { |
| 73 | + for &next in &red_nexts[node] { |
| 74 | + if answer[next].0 > length + 1 { |
| 75 | + answer[next].0 = length + 1; |
| 76 | + nodes_heap.push((Reverse(length + 1), next, true)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + answer |
| 83 | + .into_iter() |
| 84 | + .map(|(red, blue)| { |
| 85 | + if red.min(blue) == i32::MAX { |
| 86 | + -1 |
| 87 | + } else { |
| 88 | + red.min(blue) |
| 89 | + } |
| 90 | + }) |
| 91 | + .collect() |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments