1129. Shortest Path with Alternating Colors
原题链接在这里: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 <= 100red_edges.length <= 400blue_edges.length <= 400red_edges[i].length == blue_edges[i].length == 20 <= 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:
class Solution {
public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) {
Set<Integer> [][] graph = new Set[2][n];
for(int i = 0; i<n; i++){
graph[0][i] = new HashSet<>();
graph[1][i] = new HashSet<>();
}
for(int [] red : red_edges){
graph[0][red[0]].add(red[1]);
}
for(int [] blue : blue_edges){
graph[1][blue[0]].add(blue[1]);
}
int [][] res = new int[2][n];
for(int i = 1; i<n; i++){
res[0][i] = Integer.MAX_VALUE;
res[1][i] = Integer.MAX_VALUE;
}
LinkedList<int []> que = new LinkedList<int []>();
que.add(new int[] {0, 0});
que.add(new int[] {1, 0});
while(!que.isEmpty()){
int [] cur = que.poll();
int row = cur[0];
int i = cur[1];
for(int nei : graph[row][i]){
if(res[1-row][nei] == Integer.MAX_VALUE){
res[1-row][nei] = res[row][i]+1;
que.add(new int[]{1-row, nei});
}
}
}
int [] resArr = new int[n];
for(int i = 0; i<n; i++){
int min = Math.min(res[0][i], res[1][i]);
resArr[i] = min == Integer.MAX_VALUE ? -1 : min;
}
return resArr;
}
}
1129. Shortest Path with Alternating Colors的更多相关文章
- 【leetcode】1129. Shortest Path with Alternating Colors
题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1. In this graph, each edge is ei ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
随机推荐
- C++实现base64编解码
Base64是常见的加密算法,代码实现了基于C++的对于base64的编码和解码. 其中注释掉的部分为编码部分,取消注释将解码部分注释掉即可实现编码,反之可以实现解码. #include <st ...
- 如何理解 PHP的依赖注入(DI) 和 控制反转(IoC)
名词解释: IoC - Inversion of Control 控制反转 DI - Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式, ...
- LInux基础(04)项目设计一(理解链表管理协议的代码架构)
要设计好一个项目必须要有一个健全的代码框架 一个结构体内有数据域和处理数据的函数指针, 先实现管理链表的函数 增加节点 删除节点 清空链表 遍历节点对每个节点进行操作 再实现协议的注册 把对象s ...
- sqlserver通过递归查找所有下级或上级部门和用户的操作实例
--查找当前用户所在部门的所有下级包括当前部门 with cte as ( as lvl from Department union all from cte c inner join Departm ...
- 多个数据源Mongo连接
MongoDB :https://my.oschina.net/u/3452433/blog/2999501多个数据源Mongo连接:https://juejin.im/post/5cf73090f2 ...
- MSP---助力企业轻松上云
一.企业上云的总体步骤 1.传统企业级应用上云方法论概述 2.应用现代化概念 3.应用上云都需要什么 1.评估该应用是否可以上云, 2.上云需要付出多少时间和人力 3.企业级应用案例 4.生成评估报告 ...
- 2019 汽车之家java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.汽车之家等公司offer,岗位是Java后端开发,最终选择去了汽车之家. 面试了很多家公司,感觉大部分公司考察的点 ...
- P1347 排序 (拓扑排序,tarjan)
题目 P1347 排序 解析 打开一看拓扑排序,要判环. 三种情况 有环(存在矛盾) 没环但在拓扑排序时存在有两个及以上的点入度为0(关系无法确定) 除了上两种情况(关系可确定) 本来懒了一下,直接在 ...
- Nginx 脚本自动进行日志切割
随着网站的运行时间越来越长,日志文件也会随之越来越大需要对Nginx日志进行切割,下面是每周六晚上自动进行日志切割的shell脚本 脚本如下: [root@jrjs ~]# vim /usr/loca ...
- MSF MS12-020RDP漏洞攻击
Metasploit利用远程桌面协议RDP拒绝访问漏洞(MS12-020) 漏洞描述:BUGTRAQ ID: 52354 CVE ID: CVE-2012-0152 远程桌面协议(RDP, Remot ...