Algorithms Playbook
← All problems
MediumShortest paths →Similar to: Network Delay Time

How long until everyone gets the signal?

Problem

A network has n nodes numbered 1..n. Each [u, v, w] is a directed link from u to v taking w time units. A signal starts at node k. Return the time until all nodes have received it, or -1 if some node never does.

Examples

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

Output: 2

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

Output: -1

Hints

    Try it first. Every hint reveals a bit more.

    Solution