• 5132. 颜色交替的最短路径


    在一个有向图中,节点分别标记为 0, 1, ..., n-1。这个图中的每条边不是红色就是蓝色,且存在自环或平行边。

    red_edges 中的每一个 [i, j] 对表示从节点 i 到节点 j 的红色有向边。类似地,blue_edges 中的每一个 [i, j] 对表示从节点 i 到节点 j 的蓝色有向边。

    返回长度为 n 的数组 answer,其中 answer[X] 是从节点 0 到节点 X 的最短路径的长度,且路径上红色边和蓝色边交替出现。如果不存在这样的路径,那么 answer[x] = -1

    示例 1:

    输入:n = 3, red_edges = [[0,1],[1,2]], blue_edges = []
    输出:[0,1,-1]
    

    示例 2:

    输入:n = 3, red_edges = [[0,1]], blue_edges = [[2,1]]
    输出:[0,1,-1]
    

    示例 3:

    输入:n = 3, red_edges = [[1,0]], blue_edges = [[2,1]]
    输出:[0,-1,-1]
    

    示例 4:

    输入:n = 3, red_edges = [[0,1]], blue_edges = [[1,2]]
    输出:[0,1,2]
    

    示例 5:

    输入:n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]
    输出:[0,1,1]
    

    提示:

    • 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
    /**
     * @param {number} n
     * @param {number[][]} red_edges
     * @param {number[][]} blue_edges
     * @return {number[]}
     */
    var shortestAlternatingPaths = function(n, red_edges, blue_edges) {
        let blueDP = new Array(n+1).fill(0);
        let redDp = new Array(n+1).fill(0);
        
        let redMap = new Map(),blueMap = new Map();
        for(let [k,v] of red_edges){
            let arr = redMap.get(k)||[];
            arr.push(v);
            redMap.set(k,arr);
        }
        for(let [k,v] of blue_edges){
            let arr = blueMap.get(k)||[];
            arr.push(v);
            blueMap.set(k,arr);
        }
        
        let stack = [[0,0,"b"],[0,0,"r"]];
        let ans = new Array(n).fill(Infinity);
        let visitedRed=new Set();
        let visitedBlue=new Set();
        visitedRed.add(0)
        visitedBlue.add(0)
        while(stack.length>0){
            let [node,level,color] = stack.shift();
            ans[node]=Math.min(level,ans[node]);
            if(color==='r'){
                let nextColor = blueMap.get(node)||[];
                for(let c of nextColor){
                    if(!visitedRed.has(c)){
                        visitedRed.add(c);
                        stack.push([c,level+1,'b']);
                    }
                }
            }else{
                let nextColor = redMap.get(node)||[];
                for(let c of nextColor){
                    if(!visitedBlue.has(c)){
                        visitedBlue.add(c);
                        stack.push([c,level+1,'r']);
                    }
                    
                }
            }
        }
        return ans.map(i=>i===Infinity?-1:i);
    };
    /**
     * @param {number} n
     * @param {number[][]} red_edges
     * @param {number[][]} blue_edges
     * @return {number[]}
     */
    var shortestAlternatingPaths = function(n, red_edges, blue_edges) {
      let graph=Array(n).fill().map(()=>Array(n).fill(null))
      let dis=Array(n).fill().map(()=>({r:Infinity,b:Infinity}))
      dis[0].r=0
      dis[0].b=0
      for(let i=0;i<red_edges.length;i++){
        let [from,to]=red_edges[i]
        if(!graph[from][to])graph[from][to]={}
        graph[from][to].r=true
      }
      for(let i=0;i<blue_edges.length;i++){
        let [from,to]=blue_edges[i]
        if(!graph[from][to])graph[from][to]={}
        graph[from][to].b=true
      }
      
      let arr=[0]
      while(arr.length>0){
        let len=arr.length
        for(let i=0;i<len;i++){
          let from=arr.shift()
          for(let j=0;j<graph[from].length;j++){
            let to=j
            if(!graph[from][to])continue
            let {r,b}=graph[from][to]
            let needPush=false
            if(r){
              if(dis[from].b+1<dis[to].r){
                dis[to].r=dis[from].b+1
                needPush=true
              }
            }
            if(b){
              if(dis[from].r+1<dis[to].b){
                dis[to].b=dis[from].r+1
                needPush=true
              }
            }
            if(needPush){
              arr.push(to)
            }
          }
        }
      }
      let res=[]
      for(let i=0;i<dis.length;i++){
        let min=Math.min(dis[i].r,dis[i].b)
        if(min===Infinity)min=-1
        res[i]=min
      }
      // console.log(dis)
      return res
    };
  • 相关阅读:
    1024 Calendar Game
    外存管理与文件系统(1)
    1008 Gnome Tetravex
    写给想用技术改变世界的年轻人by 沃兹
    为什么VS2008里没有Silverlight呢?
    QQ消息群发器实现原理及核心代码
    JavaScript检测多组Radio是否选择
    C#操作Excel(导入导出)
    String.Split 方法有6个重载函数:
    【原】Ext2.2学习系列:ExtJS与.NET结合开发实例全部Ext2.2示例索引贴 (共收录56篇文章)
  • 原文地址:https://www.cnblogs.com/zhangzs000/p/11223438.html
Copyright © 2020-2023  润新知