Codeforces Round 438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
E
Policeman and a Tree
Codeforces
2700
原题
← DF →
翻译中…

You are given a tree (a connected non-oriented graph without cycles) with vertices numbered from $1$ to $*n*$, and the length of the $*i*$-th edge is $*w*_{*i*}$. In the vertex $*s*$ there is a policeman, in the vertices $*x*_{1}, *x*_{2}, ..., *x*_{*m*}$ ($*x*_{*j*} ≠ *s*$) $*m*$ criminals are located.

翻译中…

The policeman can walk along the edges with speed $1$, the criminals can move with arbitrary large speed. If a criminal at some moment is at the same point as the policeman, he instantly gets caught by the policeman. Determine the time needed for the policeman to catch all criminals, assuming everybody behaves optimally (i.e. the criminals maximize that time, the policeman minimizes that time). Everybody knows positions of everybody else at any moment of time.

翻译中…

Input

翻译中…

The first line contains single integer $*n*$ ($1 ≤ *n* ≤ 50$) — the number of vertices in the tree. The next $*n* - 1$ lines contain three integers each: $*u*_{*i*}$, $*v*_{*i*}$, $*w*_{*i*}$ ($1 ≤ *u*_{*i*}, *v*_{*i*} ≤ *n*$, $1 ≤ *w*_{*i*} ≤ 50$) denoting edges and their lengths. It is guaranteed that the given graph is a tree.

翻译中…

The next line contains single integer $*s*$ ($1 ≤ *s* ≤ *n*$) — the number of vertex where the policeman starts.

翻译中…

The next line contains single integer $*m*$ ($1 ≤ *m* ≤ 50$) — the number of criminals. The next line contains $*m*$ integers $*x*_{1}, *x*_{2}, ..., *x*_{*m*}$ ($1 ≤ *x*_{*j*} ≤ *n*$, $*x*_{*j*} ≠ *s*$) — the number of vertices where the criminals are located. $*x*_{*j*}$ are not necessarily distinct.

翻译中…

Output

翻译中…

If the policeman can't catch criminals, print single line "Terrorists win" (without quotes).

翻译中…

Otherwise, print single integer — the time needed to catch all criminals.

翻译中…

Note

翻译中…

In the first example one of the optimal scenarios is the following. The criminal number $2$ moves to vertex $3$, the criminal $4$ — to vertex $4$. The policeman goes to vertex $4$ and catches two criminals. After that the criminal number $1$ moves to the vertex $2$. The policeman goes to vertex $3$ and catches criminal $2$, then goes to the vertex $2$ and catches the remaining criminal.

翻译中…

样例

Input #1
4
1 2 2
1 3 1
1 4 1
2
4
3 1 4 1
Output #1
8
InputCopy OutputCopy
Input #2
6
1 2 3
2 3 5
3 4 1
3 5 4
2 6 3
2
3
1 3 5
Output #2
21
InputCopy OutputCopy
题目信息
题号
868E
时间限制
2 s
内存限制
256 MB
标签
动态规划
图论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。