All problems
0297MediumDepth-First SearchBreadth-First SearchGraph TheoryHeap (Priority Queue)Shortest PathDijkstra's Algorithm

Relay Broadcast Time

Tracked in this browser only
Write code

Trains the technique from

LeetCode 743Network Delay Time

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A courier firm runs depots sorting depots numbered 1 through depots. Its one-way shuttle legs are listed in legs, where legs[i] = [u_i, v_i, w_i] means a shuttle leaves depot u_i for depot v_i and the crossing takes w_i minutes. A leg is usable in that direction only. No two legs share the same ordered pair of depots.

At minute 0 a dispatch note is posted at depot origin. A depot forwards a copy of the note along every leg leaving it the instant the note arrives, and copies travel at the same time without interfering. A depot counts as served the first moment any copy reaches it, and the origin is served at minute 0. Some legs take 0 minutes.

Return the minute at which the last depot is served. If at least one depot is never served, return -1.

Examples

Example 1

Input
legs = [[1,2,3],[2,3,4],[1,3,9]], depots = 3, origin = 1
Output
7

Depot 1 holds the note at minute 0, depot 2 is served at minute 3, and a copy forwarded from depot 2 serves depot 3 at minute 7. The last depot is therefore served at minute 7.

Example 2

Input
legs = [[2,1,4]], depots = 2, origin = 1
Output
-1

The only leg runs from depot 2 to depot 1, and legs are one-way, so nothing ever leaves depot 1 and depot 2 stays unserved.

Constraints

  • 1 <= origin <= depots <= 100
  • 1 <= legs.length <= 6000
  • legs[i].length == 3
  • 1 <= u_i, v_i <= depots
  • u_i != v_i
  • 0 <= w_i <= 100
  • All the pairs (u_i, v_i) are unique. (i.e., no multiple legs.)

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def broadcast_time(legs: list[list[int]], depots: int, origin: int) -> int:
Java
public int broadcastTime(int[][] legs, int depots, int origin)
September 7
Apply