Network Delay Time
There areNnetwork nodes, labelled1toN.
Giventimes, a list of travel times asdirectededgestimes[i] = (u, v, w), whereuis the source node,vis the target node, andwis the time it takes for a signal to travel from source to target.
Now, we send a signal from a certain nodeK. How long will it take for all nodes to receive the signal? If it is impossible, return-1.
Example 1:

Note:
分析
DFS:从起始到每个点,记录到该点最小距离。最大的那个距离就是答案,如果有点未遍历则-1
比较带记忆的dfs是dict+dfs返回,此处是dict+dfs带参数
用dfs返回带记忆的做法,只能得到每个点最短距离,但是本题需要得到每个点最短距离之后,从起点开始最长的点。
BFS
Dijkstra's Algorithm
heap:
Last updated
Was this helpful?