#P9797. Escape The Maze
Escape The Maze
Escape The Maze
Problem Description
Alice is currently trapped in a maze, which can be seen as a . Each edge in the tree has a weight representing the length of that edge. The leaves of the tree represent the exits, and when Alice reaches a leaf, it means she has successfully escaped from the maze. A leaf is defined as a node with degree 1 that is not the root. Each maze has a difficulty level, denoted as . When Alice is at a node in the tree, she can choose to jump to a node in her subtree. Let be the sum of the edge weights along the path from to . The energy spent when jumping from to is . Alice wants to know the minimum amount of energy required to escape the maze if the tree has as the root and she starts from . Alice will ask this question a total of times. The data guarantees that for any given pair of points and , the absolute value of the sum of edge weights along the path between them does not exceed .
Input
The input consists of multiple test cases. The first line contains a single integer — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers — the number of nodes in the tree. Each of the next lines contains three integers The next line contains a positive integer 。 Each of the next lines contains one integer asks the minimum amount of energy required to escape the maze if the tree has as the root and she starts from . It is guaranteed that the given graph is a tree.
Output
For each test case, output lines. Each line should contain a integer indicating the minimum amount of energy required. The data guarantees that the answer will not exceed the range that can be represented by a 64-bit signed integer.
Sample Input
1
4 2
1 2 5
1 3 -4
1 4 6
4
1
2
3
4
Sample Output
9
1
0
0
Source
2023“钉耙编程”中国大学生算法设计超级联赛(1)