#P9875. L. Landmine
L. Landmine
L. Landmine
Problem Description
Given a tree where each edge has a length. Each node contains a landmine, and the -th node's landmine has an explosion radius of . We define as the shortest distance between vertex and vertex in the tree. In other words, is the sum of edge lengths along the unique simple path between vertex and vertex . When the landmine at vertex explodes, after one second, all landmines within distance from vertex will explode together. In other words, for all landmines satisfying , if they have not exploded yet, they will be detonated one second after the landmine at vertex explodes. For each , you want to calculate how long it will take for the first landmine at vertex to be detonated after detonating the landmine at vertex , or if it can never be detonated.
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 one integer (). The second line contains integers (). Each of the following lines contains three integers (, , ) - an edge between and with a length of . It's guaranteed that .
Output
For each test case, print integers - the time in seconds after detonating the landmine at vertex , at which the first landmine at vertex will be detonated. If it can never be detonated, output .
Sample Input
2
3
2 6
1 2 3
2 3 2
5
1 1 1 1
1 2 1
1 3 1
2 4 1
2 5 2
Sample Output
2 1
1 1 2 -1
Source
2023“钉耙编程”中国大学生算法设计超级联赛(7)