原题链接在这里:https://leetcode.com/problems/shortest-path-with-alternating-colors/
题目:
Consider a directed graph, with nodes labelled 0, 1, ..., n-1
. In this graph, each edge is either red or blue, and there could be self-edges or parallel edges.
Each [i, j]
in red_edges
denotes a red directed edge from node i
to node j
. Similarly, each [i, j]
in blue_edges
denotes a blue directed edge from node i
to node j
.
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 doesn't exist).
Example 1:
Input: n = 3, red_edges = [[0,1],[1,2]], blue_edges = [] Output: [0,1,-1]
Example 2:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[2,1]] Output: [0,1,-1]
Example 3:
Input: n = 3, red_edges = [[1,0]], blue_edges = [[2,1]] Output: [0,-1,-1]
Example 4:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[1,2]] Output: [0,1,2]
Example 5:
Input: n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]] Output: [0,1,1]
Constraints:
1 <= n <= 100
red_edges.length <= 400
blue_edges.length <= 400
red_edges[i].length == blue_edges[i].length == 2
0 <= red_edges[i][j], blue_edges[i][j] < n
题解:
To calculate the shortest path, use BFS.
Construct the graph, for each node, put its neighbors into 2 different sets based on edge colors.
res array also have 2 rows, shortest paths from different edges.
Add {0, 0}, {1, 0} into queue representing starting node with different colored edges.
When pulling out cur. cur[0] is the color going out, cur[1] is the node i. find it neighbor by graph[cur[0]][cur[1]].
For each neighbor, it needs to spread out with different color 1- cur[0]. Thus check res[1-cur[0]][nei].
If it has been visited before, it should still be Integer.MAX_VALUE, update it with current step res[cur[0]][cur[1]] + 1. And add it to queue.
Finally get the smallest steps for each of the nodes.
Note: Set<Integer> [][] graph = new Set[2][n]. Declared type must include type Integer. Constructor can't put <Integer> after Set.
Time Complexity: O(n+E). Perform 2 BFS iterations. E = red_edges.length + blue_edges.length.
Space: O(n).
AC Java:
1 class Solution { 2 public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) { 3 Set<Integer> [][] graph = new Set[2][n]; 4 for(int i = 0; i<n; i++){ 5 graph[0][i] = new HashSet<>(); 6 graph[1][i] = new HashSet<>(); 7 } 8 9 for(int [] red : red_edges){ 10 graph[0][red[0]].add(red[1]); 11 } 12 13 for(int [] blue : blue_edges){ 14 graph[1][blue[0]].add(blue[1]); 15 } 16 17 int [][] res = new int[2][n]; 18 for(int i = 1; i<n; i++){ 19 res[0][i] = Integer.MAX_VALUE; 20 res[1][i] = Integer.MAX_VALUE; 21 } 22 23 LinkedList<int []> que = new LinkedList<int []>(); 24 que.add(new int[] {0, 0}); 25 que.add(new int[] {1, 0}); 26 while(!que.isEmpty()){ 27 int [] cur = que.poll(); 28 int row = cur[0]; 29 int i = cur[1]; 30 for(int nei : graph[row][i]){ 31 if(res[1-row][nei] == Integer.MAX_VALUE){ 32 res[1-row][nei] = res[row][i]+1; 33 que.add(new int[]{1-row, nei}); 34 } 35 } 36 } 37 38 int [] resArr = new int[n]; 39 for(int i = 0; i<n; i++){ 40 int min = Math.min(res[0][i], res[1][i]); 41 resArr[i] = min == Integer.MAX_VALUE ? -1 : min; 42 } 43 44 return resArr; 45 } 46 }