P21044 [HackerRank] Jeanies route
普及
题目描述

Byteland has $N$ cities (numbered from $\mbox{1}$ to $N$) and $N-1$ bidirectional roads. It is guaranteed that there is a route from any city to any other city.

Jeanie is a postal worker who must deliver $\mbox{K}$ letters to various cities in Byteland. She can start and end her delivery route in any city. Given the destination cities for $\mbox{K}$ letters and the definition of each road in Byteland, find and print the minimum distance Jeanie must travel to deliver all $\mbox{K}$ letters.

Note: The letters can be delivered in any order.

Input Format

The first line contains two space-separated integers, $N$ (the number of cities) and $\mbox{K}$ (the number of letters), respectively.

The second line contains $\mbox{K}$ space-separated integers describing the delivery city for each letter.

Each line $\boldsymbol{i}$ of the $N-1$ subsequent lines contains $3$ space-separated integers describing a road as $u_iv_i d_i$, where $d_i$ is the distance (length) of the bidirectional road between cities $u_i$ and $v_i$.

Constraints

$2\leq K\leq N\leq10^5$
$1\leq d_i\leq10^3$
$\textit{Byteland is a weighted undirected acyclic graph.}$

Output Format

Print the minimum distance Jeanie must travel to deliver all $\mbox{K}$ letters.

Sample Input 0
5 3
1 3 4
1 2 1
2 3 2
2 4 2
3 5 3

Sample Output 0
6

Explanation 0

Jeanie has $3$ letters she must deliver to cities $\mbox{I}$, $3$, and $\begin{array}{c}A\end{array}$ in the following map of Byteland:

One of Jeanie's optimal routes is $\underbrace{3\to2}_{2}\overbrace{\to1}^{1}\underbrace{\to2}_{1}\overbrace{\to4}^{2}_{\cdot}$, for a total distanced traveled of $2+1+1+2=6$. Thus, we print $\boldsymbol{6}$ on a new line.

输入格式

见题面

输出格式

见题面

样例输入
5 3 1 3 4 1 2 1 2 3 2 2 4 2 3 5 3
样例输出
6