Signal Delay
A network has nodes 1 to n. Each edge [u, v, w] sends a signal from u to v in w time units (directed, ). 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: .
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 OUTPUT
2Start 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.