Relay Marathon
Time limit6sMemory limit512 MB
Given a weighted undirected graph and K special cities, find two pairs of distinct special cities, all four different, whose shortest-path distances sum to the minimum.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Heap
- Solved
- No attempts yet
Problem
You are Nilan, the marathon organiser of the country Berapur. There are N cities in the country, connected by M roads. The cities are indexed from 1 to N, and each road is indexed from 1 to M. The ith road connects city u_i directly with city v_i, and it takes w_i seconds to travel on it. Roads are bidirectional. There are no self-loops or multi-edges. Among the N cities, there is a list of K distinct cities A_1, A_2, ..., A_K that are special.
A relay marathon works as follows. Two groups of people start at the cities start1 and start2. First, the group at start1 travels to finish1. As soon as the first group reaches finish1, the second group starts traveling from start2 to finish2 with no delay. When the second group reaches finish2, the relay ends. A relay is valid only if start1, finish1, start2, and finish2 are all special and distinct from one another.
Let D(a, b) be the length of the shortest path from city a to city b. If no path exists, D(a, b) = . The total time of a valid relay is D(start1, finish1) + D(start2, finish2). Find the minimum total time over all valid tuples (start1, finish1, start2, finish2).
It is guaranteed that at least one valid tuple of four distinct special cities (a, b, c, d) exists with D(a, b) + D(c, d) < .
Input
The first line contains three integers N, M, and K. Each of the next M lines contains three integers u_i, v_i, and w_i, meaning a road joins cities u_i and v_i and takes w_i seconds in either direction. The last line contains the K distinct integers A_1, A_2, ..., A_K, the special cities.
Output
Print a single integer on one line: the minimum value of D(start1, finish1) + D(start2, finish2) (in seconds).
Constraints
for all
for all