#P9433. [NAPC-#1] Stage5 - Conveyors
[NAPC-#1] Stage5 - Conveyors
Background
— rs8
Problem Description
Brief Statement
You are given an undirected tree with nodes and key nodes on the tree. The edges have weights (i.e., edge lengths). There are queries. Each query gives and asks for the minimum length of a path from to that visits each key node at least once.
Original Statement
On some level, kid ran into a back-and-forth stage again. Really popular apparently.
The space left for kid by this stage is an undirected weighted tree, where the edge weight is the edge length. The tree has nodes, and there is exactly one glowing orb on each of nodes. When kid passes through a node with an orb (called a key node), he can collect the orb. Kid starts from node . When he has collected all orbs, the portal will appear at node .
Kid wants to speedrun, so he wants to know the minimum time he needs to walk from , collect all orbs, and enter the portal at (which is simply the path length, since kid’s speed is constant).
However, Geezer is very cunning. This floor of the tower was copied into layers, and the and in each layer may also change. Kid panicked and hurried to ask you for help.
Input Format
The first line contains three positive integers , representing the number of nodes in the tree, the number of queries, and the number of key nodes.
The next lines each contain three positive integers , indicating that there is an edge in the tree with weight . It is guaranteed that the input forms a tree.
The next line contains distinct positive integers, giving the indices of the key nodes.
The next lines each contain two positive integers , representing one query.
Output Format
For each query, output one line with one non-negative integer, representing the minimum length of a valid path for that query.
Note that a valid path may traverse no edges; in that case, the path length is .
7 5 2
1 2 3
1 3 5
3 4 2
3 5 4
2 6 1
1 7 1
2 3
2 3
2 1
7 1
4 5
6 6
8
13
17
22
18
Hint
Constraints
This problem uses bundled testdata.
$$\def\r{\cr\hline} \def\None{\text{None}} \def\arraystretch{1.5} \begin{array}{c|c|c|c} \textbf{Subtask} & \text{Test Point IDs} & \textbf{Sp. Constraints} & \textbf{Score}\r \textsf1&1\sim14 & n\leqslant15,\mathbf R& 18 \r \textsf2&15\sim20 & q=1 & 18 \r \textsf3&46\sim48 & s=t,k=n & 6 \r \textsf4&21\sim25 & k=n & 18 \r \textsf5&26\sim30 & \mathbf A & 18 \r \textsf6&31\sim45 & - & 22 \r \end{array}$$Friendly reminder: does not restrict the range of .
- Special property : The tree is guaranteed to be generated randomly (for , choose a random node in and connect it to , and generate the edge weight uniformly at random within a fixed interval).
- Special property : Define as: there exist (possibly ) such that both and are key nodes, and lies on the shortest path from to . Then, for each query, it is guaranteed that both and hold.
For of the data, , , , , .
Sample Explanation #1

The bold nodes in the figure are the key nodes.
For each query, the following is one optimal path (there may be multiple optimal paths):
- .
- .
- .
- .
- .
Translated by ChatGPT 5
