問題一覧 > 通常問題

No.1308 Jump Beacon

レベル : / AI推定難易度 : 2075 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 48
作問者 : やむなく / テスター : りあん
お気に入りにしたユーザー ProblemId : 2354 / 自分の提出
問題文最終更新日: 2020-12-06 13:40:16
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2020 (順位表) の他の問題:

Note

This problem has a strict execution time limit, so using a fast language is recommended. (It has been confirmed that AC is achievable with C++, C#, and PyPy3.)

Video

Jump Beacon

Problem Statement

There is a tree with $N$ vertices. The vertices of this tree are numbered from $1$ to $N$, and the edges are numbered from $1$ to $N-1$.

Edge $i (1 \le i \le N-1)$ connects vertex $u_i$ and vertex $v_i$ and has length $l_i$.

You are given a positive integer $C$ and a vertex sequence $(x_1, x_2, \ldots, x_Q)$ of length $Q$.

Ika is currently at vertex $x_1$ and is about to visit the $Q-1$ vertices $x_2, \ldots, x_Q$ in order.

Ika repeatedly chooses and performs $1$ of the following $3$ actions.

  • Choose $1$ edge incident to the current vertex and move to the other $1$ vertex connected by that edge.

    • This action takes time equal to the length $l_i$ of the chosen edge.

  • Place $1$ jump beacon on the current vertex.

    • If a beacon is already placed on the tree, it disappears, and only the $1$ beacon at the current position remains.

    • This action takes no time.

  • Move to the vertex where the jump beacon is placed.

    • The jump beacon disappears.

    • This action takes time $C$.

Find the minimum time required for Ika to visit the $Q-1$ vertices in order.

Input

$N$ $Q$ $C$
$u_1$ $v_1$ $l_1$
$u_2$ $v_2$ $l_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$ $l_{N-1}$
$x_1$ $x_2$ $\ldots$ $x_Q$
  • $2 \le N,Q \le 3{,}000$
  • $1 \le C \le 10^9$
  • $1 \le u_i, v_i \le N (1 \le i \le N-1)$
  • $1 \le l_i \le 10^9 (1 \le i \le N-1)$
  • The given graph is a tree.
  • $1 \le x_i \le N (1 \le i \le Q)$
  • $x_i \ne x_{i+1} (1 \le i \le Q-1)$
  • All input values are integers.

Output

Print the minimum time required for Ika to move through the $Q-1$ vertices in order. Print a newline at the end.

Sample

Sample 1
Input
3 5 1
1 2 1000
2 3 10
1 3 2 3 1
Output
1031

Initially, Ika is at vertex $1$. Ika performs the following actions in order.

  1. Place a jump beacon on vertex $1$.
  2. Choose edge $1$ and move to vertex $2$. This takes time $l_1 = 1000$.
  3. Choose edge $2$ and move to vertex $3$. This takes time $l_2 = 10$.
  4. Choose edge $2$ and move to vertex $2$. This takes time $l_2 = 10$.
  5. Choose edge $2$ and move to vertex $3$. This takes time $l_2 = 10$.
  6. Move to vertex $1$, where the jump beacon is placed. This takes time $C = 1$.

In time $1031$, Ika can visit vertices $3, 2, 3, 1$ in order.

There is no way of acting that takes less time than this, so the answer is $1031$.

Sample 2
Input
6 5 3
1 5 3
6 5 2
4 6 2
3 5 2
5 2 5
1 2 3 4 5
Output
22
Sample 3
Input
2 10 1
1 2 1000000000
2 1 2 1 2 1 2 1 2 1
Output
5000000004

The answer may not fit in a 32bit integer.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。