시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB38211959.375%

문제

Lilith is a hungry caterpillar! From her vantage point at the root of a tree, she has identified some leaves she wishes to munch before returning to the root. She wants to finish munching all of them as quickly as possible so that she will grow into a plump, buttery butterfly.

The tree Lilith occupies is a bit unusual. We can view it as a collection of nodes, where some of the nodes contain leaves that Lilith wishes to munch. Each branch connects exactly two nodes together. It is guaranteed that between every pair of nodes, there is precisely one way to travel from one to the other.

Given a description of the tree and which nodes have leaves that Lilith wishes to munch, can you help Lilith route her munching by minimizing the time she must travel?

Figure 1: Illustration of Sample Input $1$. Lilith can munch the nodes at leaves $2,3,6$ and return to the root $0$ by following the following sequence of nodes: $0→4→6→4→0→1→2→1→3→1→0$. The total distance Lilith travels is the length of each branch she crossed in this sequence: $2+3+3+2+5+1+1+4+4+5=30$.

입력

The first line of input contains two integers $N$ ($1≤N≤1000$), which is the number of nodes in the tree, and $K$ ($1≤K≤N$), which is the number of leaves to be munched.

The next $N-1$ lines of input describe the branches (edges) of the tree. The $i$th such line contains three integers $s_i$, $t_i$ ($0≤s_i,t_i<N$, $s_i≠t_i$), and $d_i$ ($0≤d_i≤10^6$). This indicates there is a branch between node $s_i$ and node $t_i$ which takes $d_i$ time to cross. Furthermore, if we view the tree as rooted at node $0$, we have that $s_i$ is the parent of $t_i$ (i.e. $s_i$ lies on the unique path from $0$ to $t_i$). Lilith always starts at the root node $0$.

The last line of input contains $K$ distinct integers $a_1,\dots ,a_K$ ($0≤a_i<N$), which indicates the nodes containing leaves that Lilith wants to munch.

출력

Display the length of the shortest path along the branches of the tree, starting and ending at the root, which allows Lilith to eat all leaves.

예제 입력 1

7 3
0 1 5
0 4 2
1 2 1
1 3 4
4 5 3
4 6 3
2 3 6

예제 출력 1

30

예제 입력 2

6 3
0 1 5
1 2 5
2 3 42
2 4 347
2 5 612
3 4 5

예제 출력 2

2022

예제 입력 3

3 2
0 1 0
0 2 21
1 2

예제 출력 3

42

출처

University > University of Alberta Programming Contest > UAPC 2022 M번

  • 문제를 만든 사람: Noah Weninger