Problems

Network Delay Time

medium
medium
graph
shortest-path
heap
dijkstra

Edges [u,v,w] take w time. Return when every node receives a signal sent from k, or -1 if any node is unreachable.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • 0 <= w <= 100

Follow-up

When would Bellman-Ford be necessary instead of Dijkstra?

Examples

Example 1

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

Example 2

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

Example 3

Input: times = [[1,2,1]], n = 2, k = 2
Output: -1
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.