LeetCode 题解工作台
到达目的地的第二短时间
城市用一个 双向连通 图表示,图中有 n 个节点,从 1 到 n 编号(包含 1 和 n )。图中的边用一个二维整数数组 edges 表示,其中每个 edges[i] = [u i , v i ] 表示一条节点 u i 和节点 v i 之间的双向连通边。每组节点对由 最多一条 边连通,顶点不存在连接…
3
题型
2
代码语言
3
相关题
当前训练重点
困难 · 图·搜索
答案摘要
class Solution: def secondMinimum(
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图·搜索 题型思路
题目描述
城市用一个 双向连通 图表示,图中有 n 个节点,从 1 到 n 编号(包含 1 和 n)。图中的边用一个二维整数数组 edges 表示,其中每个 edges[i] = [ui, vi] 表示一条节点 ui 和节点 vi 之间的双向连通边。每组节点对由 最多一条 边连通,顶点不存在连接到自身的边。穿过任意一条边的时间是 time 分钟。
每个节点都有一个交通信号灯,每 change 分钟改变一次,从绿色变成红色,再由红色变成绿色,循环往复。所有信号灯都 同时 改变。你可以在 任何时候 进入某个节点,但是 只能 在节点 信号灯是绿色时 才能离开。如果信号灯是 绿色 ,你 不能 在节点等待,必须离开。
第二小的值 是 严格大于 最小值的所有值中最小的值。
- 例如,
[2, 3, 4]中第二小的值是3,而[2, 2, 4]中第二小的值是4。
给你 n、edges、time 和 change ,返回从节点 1 到节点 n 需要的 第二短时间 。
注意:
- 你可以 任意次 穿过任意顶点,包括
1和n。 - 你可以假设在 启程时 ,所有信号灯刚刚变成 绿色 。
示例 1:

输入:n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 输出:13 解释: 上面的左图展现了给出的城市交通图。 右图中的蓝色路径是最短时间路径。 花费的时间是: - 从节点 1 开始,总花费时间=0 - 1 -> 4:3 分钟,总花费时间=3 - 4 -> 5:3 分钟,总花费时间=6 因此需要的最小时间是 6 分钟。 右图中的红色路径是第二短时间路径。 - 从节点 1 开始,总花费时间=0 - 1 -> 3:3 分钟,总花费时间=3 - 3 -> 4:3 分钟,总花费时间=6 - 在节点 4 等待 4 分钟,总花费时间=10 - 4 -> 5:3 分钟,总花费时间=13 因此第二短时间是 13 分钟。
示例 2:

输入:n = 2, edges = [[1,2]], time = 3, change = 2 输出:11 解释: 最短时间路径是 1 -> 2 ,总花费时间 = 3 分钟 第二短时间路径是 1 -> 2 -> 1 -> 2 ,总花费时间 = 11 分钟
提示:
2 <= n <= 104n - 1 <= edges.length <= min(2 * 104, n * (n - 1) / 2)edges[i].length == 21 <= ui, vi <= nui != vi- 不含重复边
- 每个节点都可以从其他节点直接或者间接到达
1 <= time, change <= 103
解题思路
方法一
class Solution:
def secondMinimum(
self, n: int, edges: List[List[int]], time: int, change: int
) -> int:
g = defaultdict(set)
for u, v in edges:
g[u].add(v)
g[v].add(u)
q = deque([(1, 0)])
dist = [[inf] * 2 for _ in range(n + 1)]
dist[1][1] = 0
while q:
u, d = q.popleft()
for v in g[u]:
if d + 1 < dist[v][0]:
dist[v][0] = d + 1
q.append((v, d + 1))
elif dist[v][0] < d + 1 < dist[v][1]:
dist[v][1] = d + 1
if v == n:
break
q.append((v, d + 1))
ans = 0
for i in range(dist[n][1]):
ans += time
if i < dist[n][1] - 1 and (ans // change) % 2 == 1:
ans = (ans + change) // change * change
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(N + E) because BFS visits each node and edge at most twice for minimum and second minimum times. Space complexity is O(N + E) for storing adjacency lists and the arrival times for BFS states. |
| 空间 | O(N + E) |
面试官常问的追问
外企场景- question_mark
Expect candidates to handle graph traversal and BFS correctly with dual timing states per node.
- question_mark
Check if the solution accounts for traffic signals and mandatory waiting periods.
- question_mark
Look for correct identification of the second minimum rather than just the shortest path.
常见陷阱
外企场景- error
Ignoring traffic signal delays when calculating edge traversal time.
- error
Assuming the second minimum is always the second shortest path without considering signal waits.
- error
Failing to track both minimum and second minimum times per vertex correctly.
进阶变体
外企场景- arrow_right_alt
Calculate the third minimum time with the same BFS pattern extended to track three arrival times per node.
- arrow_right_alt
Modify traversal times per edge and still find the second minimum respecting signal cycles.
- arrow_right_alt
Apply the problem to weighted graphs where edge times differ and traffic signals still apply.