ORIGINAL CODING QUESTION
Time for an announcement to reach everyone
An announcement starts at server s and travels along one-way links; a link (u,v,t) delivers it from u to v t milliseconds after u has it. Servers forward immediately. Return the moment the last server first receives the announcement, which is the largest of the shortest delivery times. Return -1 if some server never receives it; a lone server gives 0.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n m, then m triples (u,v,time), then the source s. Servers are zero-based.
Constraints
1 ≤ n ≤ 30; 0 ≤ m ≤ 100; 0 ≤ time ≤ 100; IDs in [0,n).
Example 1
Input
4 4 0 1 4 0 2 1 2 1 2 1 3 5 0
Output
8
Server 1 is reached at 3 through server 2, and server 3 at 3 + 5 = 8.
Example 2
Input
3 2 0 1 3 2 1 1 0
Output
-1
Nothing links to server 2 from the source.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Each server is reached at its shortest-path distance from the source.
Hint 2
Non-negative link times allow Dijkstra's algorithm.
Hint 3
The answer is the maximum finite distance, or -1 if any distance stays infinite.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →