Codey and Sightseeing
https://www.hackerrank.com/contests/codenection-2023-final-round-open-category/challenges/cn-c15/
Last updated
https://www.hackerrank.com/contests/codenection-2023-final-round-open-category/challenges/cn-c15/
Last updated
Codey has an adventurous spirit. Whatever catches its eye, it will take a look at. There are n
locations, m
roads, and k
of the locations that have attracted Codey's attention. Each road represents a distance of 1
. You will be given an array a
of length k
, where represents the -th place of interest.
Due to its curiosity, Codey is eager to find the shortest distance it needs to take to reach the closest place of interest from each location. Your task is to find the sum of distances from every location to its closest place of interest.
The first line contains three integers, n
, m
and k
, where n
represents the total number of locations, m
represents the number of roads and k
represents the number of places of interest.
The second line contains k
integers , each representing the -th place of interest.
The following m
lines contains two integers, u
and v
, which indicates the road between location u
and v
.
It is guaranteed that there is no self loop & multiple edges for each test case
Output an integer representing the sum of distances from every location to its closest place of interest.
There is a total distance of 3
from each location to the place of interest as follows:
For location 1, the closest place of interest is itself, therefore distance is 0.
For location 1, the closest place of interest is location 1, therefore distance is 1.
For location 3, the closest place of interest is location 1, therefore distance is 2.
All are unique
Therefore, the sum of distances from each location to its closest place of interest is .