原文链接: https://leetcode-cn.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
中文题目
在一个有向图中,节点分别标记为 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
通过代码
高赞题解
- main idea:BFS,从节点0起,每次take one step,这样保证到达每个节点时路径是最短的。而我们并不知道节点0到达节点i的最短路径,是’红蓝红…’还是’蓝红蓝…’,所以我们需要都找出来,用nx2的数组
dist
保存,最后再选短的那个。
<,,,,>
class Solution:
def shortestAlternatingPaths(self, n: int, red_edges: List[List[int]], blue_edges: List[List[int]]) -> List[int]:
red_path = [set() for i in range(n)]
blue_path = [set() for i in range(n)]
dist = [[None, None] for i in range(n)]
dist[0] = [0, 0]
step = 0
now_red = [0]
now_blue = [0]
for start, end in red_edges:
red_path[start].add(end)
for start, end in blue_edges:
blue_path[start].add(end)
# step 1 找到分别以红边开始和以蓝边开始的两条最短路径
while len(now_red) != 0 or len(now_blue) != 0 :
new_red, new_blue = [], []
step += 1
if len(now_blue) != 0:
for point in now_blue:
for next_point in red_path[point]:
if dist[next_point][0] is None:
new_red.append(next_point)
dist[next_point][0] = step
if len(now_red) != 0:
for point in now_red:
for next_point in blue_path[point]:
if dist[next_point][1] is None:
new_blue.append(next_point)
dist[next_point][1] = step
now_red, now_blue = new_red, new_blue
# step 2 在这两条最短路径中选择小的,merge成我们的答案
ans = []
for i in range(n):
if dist[i][0] is None and dist[i][1] is None:
ans.append(-1)
elif dist[i][0] is not None and dist[i][1] is not None:
ans.append(min(dist[i][0], dist[i][1]))
elif dist[i][0] is not None:
ans.append(dist[i][0])
else:
ans.append(dist[i][1])
return ans
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
6546 | 17082 | 38.3% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|