Formatted question description: https://leetcode.ca/all/1129.html

1129. Shortest Path with Alternating Colors

Level

Medium

Description

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

Solution

The idea is to use breadth first search. However, since there are two kinds of edges that have different colors and it is required that the edge colors altenate along the path, how to represent the states should be considered carefully.

In traditional breadth first search, only one state needs to be maintained for each node. However, in this problem, for each node there are two possible starting paths and two possible current paths, so four states need to be maintained for each node.

Initialize all nodes to unvisited and all distances to Integer.MAX_VALUE. For each node, store its starting path and current path as well, so each element is represented as an array [nodeIndex, startPath, curPath]. The two initial elements that are offered to the queue are [0, RED, RED] and [0, BLUE, BLUE]. Each time an element is polled from the queue, obtain all possible next nodes using curPath, obtain nextPath that is different from curPath, and update the next nodes’ states that are reachable currently (that is, the state that uses startPath and nextPath).

After all the nodes are visited, for each node, obtain its minimum possible distance, and return the distances.

class Solution {
    public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) {
        Map<Integer, List<Integer>> redEdgesMap = new HashMap<Integer, List<Integer>>();
        for (int[] redEdge : red_edges) {
            int src = redEdge[0], dst = redEdge[1];
            List<Integer> dstList = redEdgesMap.getOrDefault(src, new ArrayList<Integer>());
            dstList.add(dst);
            redEdgesMap.put(src, dstList);
        }
        Map<Integer, List<Integer>> blueEdgesMap = new HashMap<Integer, List<Integer>>();
        for (int[] blueEdge : blue_edges) {
            int src = blueEdge[0], dst = blueEdge[1];
            List<Integer> dstList = blueEdgesMap.getOrDefault(src, new ArrayList<Integer>());
            dstList.add(dst);
            blueEdgesMap.put(src, dstList);
        }
        final int WHITE = 0;
        final int GRAY = 1;
        final int BLACK = 2;
        final int RED = 0;
        final int BLUE = 1;
        int[][] colors = new int[n][4];
        int[][] distances = new int[n][4];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 4; j++)
                distances[i][j] = Integer.MAX_VALUE;
        }
        colors[0][0] = GRAY;
        colors[0][3] = GRAY;
        distances[0][0] = 0;
        distances[0][3] = 0;
        Queue<int[]> queue = new LinkedList<int[]>();
        for (int i = 0; i < 2; i++)
            queue.offer(new int[]{0, i, i});
        while (!queue.isEmpty()) {
            int[] indexPath = queue.poll();
            int index = indexPath[0], startPath = indexPath[1], path = indexPath[2];
            int distance = distances[index][startPath * 2 + path];
            List<Integer> nextNodes = path == RED ? redEdgesMap.getOrDefault(index, new ArrayList<Integer>()) : blueEdgesMap.getOrDefault(index, new ArrayList<Integer>());
            int nextPath = RED + BLUE - path;
            for (int nextNode : nextNodes) {
                if (colors[nextNode][startPath * 2 + nextPath] == WHITE) {
                    colors[nextNode][startPath * 2 + nextPath] = GRAY;
                    distances[nextNode][startPath * 2 + nextPath] = distance + 1;
                    queue.offer(new int[]{nextNode, startPath, nextPath});
                }
            }
            colors[index][startPath * 2 + path] = BLACK;
        }
        int[] shortestDistances = new int[n];
        for (int i = 0; i < n; i++) {
            int shortestDistance = Integer.MAX_VALUE;
            for (int j = 0; j < 4; j++)
                shortestDistance = Math.min(shortestDistance, distances[i][j]);
            shortestDistances[i] = shortestDistance == Integer.MAX_VALUE ? -1 : shortestDistance;
        }
        return shortestDistances;
    }
}

All Problems

All Solutions