Find how long a signal takes to reach every node in a weighted directed network

Quick Overview

Given a directed graph with nonnegative travel times on its edges and a start node, compute how long a signal takes to reach every node, or return -1 when some node can never be reached. It tests single-source shortest paths on weighted graphs and the use of a priority queue.

Find how long a signal takes to reach every node in a weighted directed network

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A network has `n` nodes labeled from `1` to `n`. You are given `times`, a list of travel times as directed edges, where `times[i] = [u, v, w]` means a signal sent from node `u` reaches node `v` after `w` units of time. A signal is sent from a given node `k`. Return the minimum time it takes for all `n` nodes to receive the signal. If it is impossible for all `n` nodes to receive the signal, return `-1`. ### Function Signature ```python def network_delay_time(times: list[list[int]], n: int, k: int) -> int: ``` ### Rules - Edges are directed: `[u, v, w]` carries the signal from `u` to `v` only. - A node that receives the signal forwards it along all of its outgoing edges at once, so each node receives the signal at the length of its shortest path from `k`. - Node `k` receives the signal at time `0`. The answer is the largest of the arrival times over all `n` nodes. ### Constraints - `1 <= k <= n <= 100` - `1 <= len(times) <= 6000` - `len(times[i]) == 3` - `1 <= u, v <= n` - `u != v` - `0 <= w <= 100` - All pairs `(u, v)` are unique, so there are no parallel edges. ### Examples ```text Input: times = [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n = 4, k = 2 Output: 2 ``` Starting from node 2, node 1 is reached at time 1, node 3 at time 1, and node 4 at time 2 through node 3. The last arrival is at time 2. ```text Input: times = [[1, 2, 1]], n = 2, k = 2 Output: -1 ``` Node 2 has no outgoing edge, so node 1 never receives the signal. ```text Input: times = [[1, 2, 4], [1, 3, 1], [3, 2, 1]], n = 3, k = 1 Output: 2 ``` Node 3 receives the signal at time 1. Node 2 receives it at time 2 by going through node 3, which is earlier than the direct edge's time of 4. The largest arrival time is 2.

Overview: Given a directed graph with nonnegative travel times on its edges and a start node, compute how long a signal takes to reach every node, or return -1 when some node can never be reached. It tests single-source shortest paths on weighted graphs and the use of a priority queue.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A network has n nodes labeled from 1 to n. You are given times, a list of travel times as directed edges, where times[i] = [u, v, w] means a signal sent from node u reaches node v after w units of time.

A signal is sent from a given node k. Return the minimum time it takes for all n nodes to receive the signal. If it is impossible for all n nodes to receive the signal, return -1.

Function Signature

def network_delay_time(times: list[list[int]], n: int, k: int) -> int:

Rules

  • Edges are directed: [u, v, w] carries the signal from u to v only.
  • A node that receives the signal forwards it along all of its outgoing edges at once, so each node receives the signal at the length of its shortest path from k .
  • Node k receives the signal at time 0 . The answer is the largest of the arrival times over all n nodes.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= len(times) <= 6000
  • len(times[i]) == 3
  • 1 <= u, v <= n
  • u != v
  • 0 <= w <= 100
  • All pairs (u, v) are unique, so there are no parallel edges.

Examples

Input: times = [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n = 4, k = 2
Output: 2

Starting from node 2, node 1 is reached at time 1, node 3 at time 1, and node 4 at time 2 through node 3. The last arrival is at time 2.

Input: times = [[1, 2, 1]], n = 2, k = 2
Output: -1

Node 2 has no outgoing edge, so node 1 never receives the signal.

Input: times = [[1, 2, 4], [1, 3, 1], [3, 2, 1]], n = 3, k = 1
Output: 2

Node 3 receives the signal at time 1. Node 2 receives it at time 2 by going through node 3, which is earlier than the direct edge's time of 4. The largest arrival time is 2.