Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Language: Journey
Description There are n cities in Byteland (numbered from 1 to n), connected by bidirectional roads. The king of Byteland is not very generous, so there are only n-1 roads, but they connect the cities in such a way that it is possible to travel from each city to any other city.
One day, a traveller Byterider arrived in the city number k. He was planning to make a journey starting in the city k and visiting on his way cities m1 , m2 , ..., mj (not necessarily in this order) --the numbers m i are all different and they are also different from k. Byterider -- like every traveller -- has only a limited amount of money, so he would like to visit all the cities that he has planned to visit using the shortest possible path (starting in the city k). A path is one road or a sequence of roads, where every next road begins in the city where the previous one ends. Help Byterider to determine the length of the shortest path for his journey. Input The first line of the standard input contains two integers n and k separated by a single space (2 <= n <= 50000, 1 <= k <= n), n is the number of cities in Byteland and k is the number of the first city on Byterider's path. Each of the following n-1 lines contains the description of one road in Byteland. Line (i + 1) (for 1 <= i <= n-1) contains three integers ai , bi and di separated by single spaces (1 <= ai ; bi <= n, 1 <= di <= 1000), ai and bi are the cities connected by the road, and di is the length of the road. Line (n + 1) contains one integer j -- the number of cities which Byterider would like to visit (1 <= j <= n-1). The next line contains j different integers m i separated by single spaces -- the numbers of the cities that Byterider would like to visit (1 <= mi <= n, mi != k). Output The first and only line of the standard output should contain exactly one integer: the length of the shortest path for Byterider's journey. Sample Input 4 2 1 2 1 4 2 2 2 3 3 2 1 3 Sample Output 5 Source |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator