Network Delay Time

A network has n nodes labelled 1 to n. You are given a list times of directed edges: times[i] = [u, v, w] means a signal sent from node u arrives at node v after w units of time. There may be more than one edge from u to v.

A signal is sent from node k at time 0. Every node passes the signal on along all of its outgoing edges as soon as it first receives it.

Return the time at which the last node receives the signal. If some node can never receive it, return -1.

Constraints

  • 1 <= n <= 100
  • 0 <= times.length <= 6000
  • 1 <= u, v <= n and u !== v
  • 0 <= w <= 100
  • 1 <= k <= n

Examples

Example 1

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

From node 1: node 3 at time 1, node 2 at time 3 (through 3, faster than the direct edge of 4), node 4 at time 4. Everyone has the signal at time 4.

Example 2

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

Edges are one-way. The signal starts at node 2 and can't travel back to node 1, so not every node receives it: -1.

Example 3

Input: times = [], n = 1, k = 1
Output: 0

The only node is the source, which has the signal at time 0.

JavaScript you'll use: Heaps and priority queues Graphs and grids in JavaScript

/**
 * @param {number[][]} times
 * @param {number} n
 * @param {number} k
 * @return {number}
 */
function networkDelayTime(times, n, k) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.