InferenceAI

Enter at least two characters to search.

Start free

Signal Delay

mediumGraphs25 min25 XP

A network has nodes 1 to n. Each edge [u, v, w] sends a signal from u to v in w time units (directed, w≥0w \ge 0). A signal starts at node k. Return the time until every node has received it, or -1 if some node is unreachable.

Requirements

  • Use Dijkstra's algorithm with a min-heap: O((V+E)log⁡V)O((V + E) \log V).

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[[2,1,1],[2,3,1],[3,4,1]],4,2]
TASKsignal_delay()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

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

Output: 2

Explanation: classic

Input: edges = [[1,2,1]], n = 2, k = 2

Output: -1

Explanation: unreachable

Input: edges = [], n = 1, k = 1

Output: 0

Explanation: single node

Hint 1

The answer is the largest shortest-path distance from k.

Hint 2

Pop the closest unsettled node from the heap and relax its edges.

Topics

DSAGraphsPython