Fastest Signal Through a Network
A network has n machines, numbered from 0 to n − 1, joined by one-way links given as three lists of the same length: a link sends a signal from machine source[i] to machine target[i], and it takes delay[i] minutes to arrive.
A signal leaves machine start at minute 0. Every machine that receives it passes it on at once along each of its links. Return the minute at which every machine has received the signal, or -1 if some machine never does.
Examples
n = 4, source = [1, 1, 2], target = [0, 2, 3], delay = [1, 1, 1], start = 1 → 2
n = 2, source = [0], target = [1], delay = [5], start = 1 → -1
n = 3, source = [0, 0, 1], target = [1, 2, 2], delay = [4, 9, 2], start = 0 → 6
n = 1, source = [], target = [], delay = [], start = 0 → 0In the third example the signal reaches machine 2 sooner through machine 1, 4 + 2 = 6, than along the direct link, 9. In the second, the only link runs from machine 0 to machine 1, and the signal starts at 1, so machine 0 never receives it.
Constraints
- Between 1 and 5,000 machines and up to 10,000 links, each with a delay between 0 and 1,000 minutes; every machine number is between
0andn − 1. - Aim for O((n + m) log n): picking the next machine by scanning them all is O(n²).
Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.