Shopping Malls
InterviewTime limit1sMemory limit128 MB
Given a connected weighted graph with some cities holding malls, find the maximum over all points on roads of the distance to the nearest mall, then round to the nearest integer.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Greedy, Math
- Solved
- No attempts yet
Problem
A country has cities connected by bidirectional roads. Exactly of these cities contain a shopping mall, and residents travel along the roads to a city with a mall in order to shop.
A house may sit inside a city, or at any point along a road. The distance from a house to the malls is defined as the shortest distance from that house to the nearest mall. People always take a shortest path, and moving within a city takes time.
Given the roads and the cities that contain a mall, write a program that finds the distance to the house that is farthest from the malls — that is, the maximum, over every possible house location, of the shortest distance from that house to the nearest shopping mall.
Input
The first line contains the number of cities , the number of roads , and the number of cities that contain a mall . Cities are numbered from to . (, , )
Each of the next lines contains a road , , : a road of length () connecting city and city . and are different, and at most one road connects any pair of cities. Every city is reachable from every other city (the graph is connected).
Each of the next lines contains the number of one city that has a mall, one per line; these numbers are distinct.
Output
Print the distance to the house that is farthest from the malls, rounded to the nearest integer (round halves up).
Hint
Suppose a house lies on the road of length between city and city , at distance () from city . Then the distance from this house to the nearest mall is , where is the shortest distance from city to the nearest mall. The point that maximizes this value gives a distance of .
In the first example, every road has length and the only mall is in city . The house farthest from the mall lies on the road between city and city , at distance from city ; its distance to the mall is , which rounds to .
