【Leetcode】1129. Shortest Path with Alternating Colors

题目地址:

https://leetcode.com/problems/shortest-path-with-alternating-colors/

给定一个 n n n阶有向图,顶点编号是 0 ∼ n − 1 0\sim n-1 0n1,每个边有个颜色,或者是红色或者蓝色。返回一个数组 A A A,使得 A [ k ] A[k] A[k]是从 0 0 0到顶点 k k k的路径中,相邻边颜色不同且长度最短的那条路径的长度。如果走不到则赋值为 − 1 -1 1

思路是BFS。在BFS的该图的时候,需要加一个维度的状态,即到达某个顶点之前走的边的颜色是什么。我们可以想象成是在一个扩充状态的隐式图上进行BFS的,每个状态有顶点编号以及入边颜色两个性质(之所以可以这么规定隐式图的顶点,是因为任意一条原图中的最短路和一条隐式图的最短路是一一对应的并且路径长度相等,关键原因是原图的最短路中某个顶点的所有红色入边最多会走其中一条,蓝色入边也类似。这可以反证法证明,如果在某个原图的最短路里,某个顶点的红色入边走了两条,那么可以将第一条红色入边和第二条红色入边的下一条蓝边之间的所有边都去掉,这样能得到一条更短路,就矛盾了)。代码如下:

import java.util.*;

public class Solution {
    
    
    public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) {
    
    
    	// 用邻接表建图
        Map<Integer, List<Integer>>[] maps = (Map<Integer, List<Integer>>[]) (new HashMap[2]);
        maps[0] = new HashMap<>();
        maps[1] = new HashMap<>();
        for (int[] red_edge : red_edges) {
    
    
            int from = red_edge[0], to = red_edge[1];
            maps[0].putIfAbsent(from, new ArrayList<>());
            maps[0].get(from).add(to);
        }
        for (int[] blue_edge : blue_edges) {
    
    
            int from = blue_edge[0], to = blue_edge[1];
            maps[1].putIfAbsent(from, new ArrayList<>());
            maps[1].get(from).add(to);
        }
        
        int[] res = new int[n];
        Arrays.fill(res, -1);
        
        Queue<int[]> queue = new ArrayDeque<>();
        queue.offer(new int[]{
    
    0, -1});
        res[0] = 0;
        
        // visited[i][0]指的是从红色边到达顶点i的这个状态有没有经历过
        boolean[][] visited = new boolean[n][2];
        
        int step = 0;
        while (!queue.isEmpty()) {
    
    
            step++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
    
    
                int[] cur = queue.poll();
                int pos = cur[0], color = cur[1];
                // 枚举下面一条路径的颜色
                for (int j = 0; j < 2; j++) {
    
    
                	// 如果颜色与上一条边颜色相同,则略过
                    if (j == color) {
    
    
                        continue;
                    }
    
                    if (maps[j].containsKey(pos)) {
    
    
                        for (int next : maps[j].get(pos)) {
    
    
                            if (!visited[next][j]) {
    
    
                                queue.offer(new int[]{
    
    next, j});
                                visited[next][j] = true;
                                // 第一次访问到next的时候就是最短距离
                                if (res[next] == -1) {
    
    
                                    res[next] = step;
                                }
                            }
                        }
                    }
                }
            }
        }
        
        return res;
    }
}

时空复杂度 O ( V + E ) O(V+E) O(V+E)

猜你喜欢

转载自blog.csdn.net/qq_46105170/article/details/112761089