CF1859F.Teleportation in Byteland
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n cities in Byteland, some of which are connected by roads, which can be traversed in any direction. The i -th road has its own hardness parameter wi . Time spent on traversing a road with its hardness equal to wi is ⌈cwi⌉ , where c is the current driving skill.
The travel network of Byteland is a tree. In other words, between any pair of cities, there is exactly one path that passes through each city at most once.
In some cities you can visit driving courses. A single course takes T time to complete, and after completing the course the driver's skill c is increased by 2 times. Notice that the time T required to complete a course is the same in all cities, and courses can be completed in the same city more than once.
You need to answer the q queries: what is the minimum time it takes to get from the city a to city b if you start the travelling with driving skill c=1 ?
输入格式
Each test contains multiple test cases. The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and T ( 1≤n≤105,1≤T≤106 ) - the number of cities and time required to complete a single driving course.
The following n−1 lines each contain three integers ui , vi and wi ( 1≤ui,vi≤n,1≤wi≤106,ui=vi ), which mean that there exists a road connecting towns ui and vi with hardness equal to wi .
The next line contains a binary string s of length n , consisting only of symbols 0 and 1 . If si=1 ( 1≤i≤n ), then you can visit driving courses in the i -th city. If si=0 ( 1≤i≤n ), then you cannot visit driving courses in the i -th city.
The next line contains a single integer q ( 1≤q≤105 ) — the number of queries you are required to answer.
The next q lines contain two integers aj , bj ( 1≤aj,bj≤n,1≤j≤q ) — the cities you are required to process in the j -th query.
It is guaranteed that the given graph is a tree. It is guaranteed that the sum of n and the sum of q over all test cases does not exceed 105 .
输出格式
For each query, print one integer in a separate line — the minimum time it takes to get in the corresponding query.
输入输出样例
输入#1
2 2 3 1 2 1 11 1 1 2 5 3 1 4 5 1 3 8 2 3 8 4 5 10 11001 5 1 5 2 5 5 1 3 4 4 2
输出#1
1 11 14 11 13 15
说明/提示
In the only query of the first test case, it is optimal to ignore the driving courses. Then the minimum time required is equal to the distance between vertexes 1 and 2 , which is 1 .
In the first query of the second test case, we can spend 3 time in city number 1 visiting the driving courses, then go to vertex 5 . Then the minimum time required is 3+⌈25⌉+⌈210⌉=11 .