LeetCode 题解工作台

网络延迟时间

有 n 个网络节点,标记为 1 到 n 。 给你一个列表 times ,表示信号经过 有向 边的传递时间。 times[i] = (u i , v i , w i ) ,其中 u i 是源节点, v i 是目标节点, w i 是一个信号从源节点传递到目标节点的时间。 现在,从某个节点 K 发出一个信…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 图·DFS·traversal

bolt

答案摘要

我们定义 表示节点 到节点 的边权,如果节点 到节点 之间没有边,则 $\textit{g}[u][v] = +\infty$。 我们维护一个数组 ,其中 表示节点 到节点 的最短路径长度。初始时,我们将 全部初始化为 ,但 $\textit{dist}[k - 1] = 0$。定义一个数组 ,其中 表示节点 是否被访问过,初始时,我们将 全部初始化为 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 图·DFS·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

n 个网络节点,标记为 1 到 n

给你一个列表 times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, wi 是一个信号从源节点传递到目标节点的时间。

现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1

 

示例 1:

输入:times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
输出:2

示例 2:

输入:times = [[1,2,1]], n = 2, k = 1
输出:1

示例 3:

输入:times = [[1,2,1]], n = 2, k = 2
输出:-1

 

提示:

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • times[i].length == 3
  • 1 <= ui, vi <= n
  • ui != vi
  • 0 <= wi <= 100
  • 所有 (ui, vi) 对都 互不相同(即,不含重复边)
lightbulb

解题思路

方法一:朴素 Dijkstra 算法

我们定义 g[u][v]\textit{g}[u][v] 表示节点 uu 到节点 vv 的边权,如果节点 uu 到节点 vv 之间没有边,则 g[u][v]=+\textit{g}[u][v] = +\infty

我们维护一个数组 dist\textit{dist},其中 dist[i]\textit{dist}[i] 表示节点 kk 到节点 ii 的最短路径长度。初始时,我们将 dist[i]\textit{dist}[i] 全部初始化为 ++\infty,但 dist[k1]=0\textit{dist}[k - 1] = 0。定义一个数组 vis\textit{vis},其中 vis[i]\textit{vis}[i] 表示节点 ii 是否被访问过,初始时,我们将 vis[i]\textit{vis}[i] 全部初始化为 false\text{false}

我们每次找到未被访问的距离最小的节点 tt,然后以节点 tt 为中心进行松弛操作,即对于每个节点 jj,如果 dist[j]>dist[t]+g[t][j]\textit{dist}[j] > \textit{dist}[t] + \textit{g}[t][j],则更新 dist[j]=dist[t]+g[t][j]\textit{dist}[j] = \textit{dist}[t] + \textit{g}[t][j]

最后,我们返回 dist\textit{dist} 中的最大值,即为答案。如果答案为 ++\infty,则说明存在无法到达的节点,返回 1-1

时间复杂度 O(n2+m)O(n^2 + m),空间复杂度 O(n2)O(n^2)。其中 nnmm 分别为节点数和边数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
        g = [[inf] * n for _ in range(n)]
        for u, v, w in times:
            g[u - 1][v - 1] = w
        dist = [inf] * n
        dist[k - 1] = 0
        vis = [False] * n
        for _ in range(n):
            t = -1
            for j in range(n):
                if not vis[j] and (t == -1 or dist[t] > dist[j]):
                    t = j
            vis[t] = True
            for j in range(n):
                dist[j] = min(dist[j], dist[t] + g[t][j])
        ans = max(dist)
        return -1 if ans == inf else ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate is expected to demonstrate knowledge of graph traversal methods and the ability to optimize with a priority queue or BFS.

  • question_mark

    Be prepared for candidates to compare DFS and BFS as options, with an emphasis on using the most efficient method for large input sizes.

  • question_mark

    A successful answer should include handling edge cases, such as unreachable nodes or cycles, and the reasoning for selecting the approach used.

warning

常见陷阱

外企场景
  • error

    Failing to correctly handle the case where not all nodes can receive the signal, leading to an incorrect return value of -1.

  • error

    Incorrectly assuming all graphs are uniform, which may lead to inefficient solutions or improper use of BFS/Dijkstra.

  • error

    Not accounting for the possibility of cycles in the graph, which could lead to an infinite loop or incorrect time calculations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    A variation of this problem could involve multiple source nodes for the signal, testing the candidate's ability to modify their approach to handle additional starting points.

  • arrow_right_alt

    Another variant could involve a directed graph with negative edge weights, which would require considering algorithms like Bellman-Ford instead of Dijkstra or BFS.

  • arrow_right_alt

    The problem could also be modified by adding additional constraints, such as limiting the number of allowed edges, which could test the candidate's ability to optimize graph traversal techniques.

help

常见问题

外企场景

网络延迟时间题解:图·DFS·traversal | LeetCode #743 中等