A monitoring signal spreads through a network of n hubs, numbered 0 to n - 1, along
m one-way links. Link i goes from hub u to hub v and takes w time units to propagate the
signal from u to v. The signal originates at a given source hub at time 0. A hub other than the
source has "received" the signal once the fastest possible propagation delay from the source to that
hub has elapsed.
Given a deadline D, print the number of hubs (not counting the source itself) that have received the
signal at or before time D.
Line 1: two integers n m.
Next m lines: three integers u v w -- a directed link from hub u to hub v with propagation
delay w (hubs are 0-indexed).
Last line: two integers s D -- the source hub and the deadline.
A single integer: the number of hubs other than s whose fastest propagation delay from s is at
most D.
Example 1
Input
4 4 0 1 3 0 2 10 1 2 2 2 3 4 0 5
Expected
2
Explanation
From hub 0, hub 1 is reached at time 3 and hub 2 at time 5 (via hub 1, cheaper than the direct link costing 10); hub 3 isn't reached until time 9. Within deadline 5, exactly 2 other hubs (1 and 2) have been reached.
Example 2
Input
3 1 0 1 5 0 2
Expected
0
Explanation
Hub 1 isn't reached until time 5, after the deadline of 2, and hub 2 has no connecting link at all, so 0 hubs are reached within the deadline.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →