Journey
InterviewTime limit1sMemory limit128 MB
Given a weighted tree, a start city k, and a set of target cities, find the length of the shortest walk from k that visits every target at least once.
Statement
There are cities in Byteland (numbered from to ), connected by bidirectional roads. There are only roads, yet they connect the cities so that it is possible to travel from any city to any other city (in other words, the cities and roads form a tree).
A traveller named Byterider arrived in city number . He plans a journey that starts in city and passes through the cities he wants to visit (in any order). These city numbers are all distinct and all different from . Byterider has only a limited amount of money, so he wants to visit all the planned cities using the shortest possible path (starting in city ). A path is a single road or a sequence of roads, where each next road starts in the city where the previous one ends. Determine the length of the shortest path for Byterider's journey.
Write a program which
- reads from standard input:
- the description of the roads connecting the cities of Byteland,
- the number of the city where Byterider arrived,
- the list of cities Byterider would like to visit,
- computes the minimum length of Byterider's journey,
- writes the result to standard output.
Input
The first line contains two integers and separated by a single space (, ), where is the number of cities and is the number of the first city on Byterider's path. Each of the next lines describes one road. The -th of these lines () contains three integers , , and separated by single spaces (, ); and are the cities connected by the road, and is its length. The next line contains one integer , the number of cities Byterider would like to visit (). The following line contains distinct integers separated by single spaces, the numbers of the cities Byterider wants to visit (, ).
Output
Print a single integer: the length of the shortest path for Byterider's journey.
Hint
