Journey
Memory limit: 32 MB
There are cities in Byteland (numbered from to ),
connected by bidirectional roads.
The king of Byteland is not very generous, so there are only
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 .
He was planning to make a journey starting in the city and
visiting on his way cities , , ...,
(not necessarily in this order) - the numbers are
all different and they are also different from .
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 ).
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.
Task
Write a program which:
-
reads from the standard input:
-
the description of the roads connecting the cities of Byteland,
-
the number of the city where Byterider arrived,
-
a list of cities which Byterider would like to visit
-
determines the minimum length of Byterider's journey,
-
writes the result to the standard output
Input
The first line of the standard input contains two integers and
separated by a single space (, ),
is the number of cities in Byteland
and is the number of the first city on Byterider's path.
Each of the following lines contains the description of one road
in Byteland.
Line (for ) contains three integers
, and separated by single spaces
(, ),
and are the cities connected by the road, and
is the length of the road.
Line contains one integer -
the number of cities which Byterider would like to visit
().
The next line contains different integers
separated by single spaces -
the numbers of the cities that Byterider would like to visit
(, ).
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.
Example
For the input data:
4 2
1 2 1
4 2 2
2 3 3
2
1 3
the correct result is:
5
Task author: Jakub Radoszewski.