Max distance
In the city, there are N districts (numbered from 0 to N-1) connected with M streets. The connections are described by two arrays, A and B, both of length M. A pair (A[K], B[K]) marks a street between districts A[K] and B[K] (for K from 0 to M-1). There are also L hospitals whose locations are described by an array H. The J-th hospital is placed in district H[J] (for J from 0 to L-1).
If an ambulance is needed in a district, one is sent from the hospital from which it will arrive in the shortest time. The ambulance arrives by the shortest possible route; passing one street takes exactly 1 minute.
Potentially, there might be a patient in need in any district of the city. What is the longest time required to reach any possible patient with an ambulance?
If some district cannot be reached with an ambulance, the function should return -1.
Examples
Example 1:
Explanation: District 5 has the longest waiting time. The ambulance will arrive from the hospital in district 2 in three minutes via route 2 → 1 → 0 → 5.
Example 2:
Example 3:
Explanation: District 5 is not connected with any district that has a hospital.
Example 4:
Explanation: There is a hospital in every district.
Constraints
N is an integer within the range [1..100,000].
M is an integer within the range [0..100,000].
L is an integer within the range [1..N].
The elements of H are all distinct.
Each element of arrays A, B, and H is an integer within the range [0..N-1].
Every street goes between two different districts.
There are no multiple streets between two districts.
Answer
Last updated