CF1859F.Teleportation in Byteland

NOI/NOI+/CTSC

通过率:0%

时间限制:8.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn cities in Byteland, some of which are connected by roads, which can be traversed in any direction. The ii-th road has its own hardness parameter wiw_i. Time spent on traversing a road with its hardness equal to wiw_i is ⌈wic⌉\lceil\frac{w_i}{c}\rceil, where cc 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 TT time to complete, and after completing the course the driver's skill cc is increased by 22 times. Notice that the time TT 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 qq queries: what is the minimum time it takes to get from the city aa to city bb if you start the travelling with driving skill c=1c = 1?

Byteland 有 nn 座城市,其中部分城市通过双向道路相连。第 ii 条道路具有自身的硬度参数 wiw_i。 traversing 一条硬度为 wiw_i 的道路所需时间为 ⌈wic⌉\lceil\frac{w_i}{c}\rceil,其中 cc 为当前驾驶技能。

Byteland 的交通网络是一棵树。换言之,任意两座城市之间恰好存在唯一一条路径,且该路径至多经过每座城市一次。

在某些城市中可以参加驾驶培训课程。每门课程耗时 TT,完成之后驾驶员的技能 cc 将变为原来的 22 倍。注意:所有城市的课程耗时 TT 相同,且同一城市可多次参加课程。

你需要回答 qq 个查询:若从城市 aa 出发前往城市 bb,且初始驾驶技能 c=1c = 1,则所需的最少时间是多少?

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and TT (1≤n≤105,1≤T≤1061 \le n \le 10^5, 1 \le T \le 10^6) - the number of cities and time required to complete a single driving course.

The following n−1n - 1 lines each contain three integers uiu_i, viv_i and wiw_i (1≤ui,vi≤n,1≤wi≤106,ui≠vi1 \le u_i, v_i \le n, 1 \le w_i \le 10^6, u_i \neq v_i), which mean that there exists a road connecting towns uiu_i and viv_i with hardness equal to wiw_i .

The next line contains a binary string ss of length nn, consisting only of symbols 00 and 11. If si=1s_i = 1 (1≤i≤n1 \le i \le n), then you can visit driving courses in the ii-th city. If si=0s_i = 0 (1≤i≤n1 \le i \le n), then you cannot visit driving courses in the ii-th city.

The next line contains a single integer qq (1≤q≤1051 \le q \le 10^5) — the number of queries you are required to answer.

The next qq lines contain two integers aja_j, bjb_j (1≤aj,bj≤n,1≤j≤q1 \le a_j, b_j \le n, 1 \le j \le q) — the cities you are required to process in the jj-th query.

It is guaranteed that the given graph is a tree. It is guaranteed that the sum of nn and the sum of qq over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 TT(1≤n≤105, 1≤T≤1061 \le n \le 10^5,\ 1 \le T \le 10^6),分别表示城市的数量以及完成一次驾驶课程所需的时间。

接下来的 n−1n - 1 行,每行包含三个整数 uiu_i、viv_i 和 wiw_i(1≤ui,vi≤n, 1≤wi≤106, ui≠vi1 \le u_i, v_i \le n,\ 1 \le w_i \le 10^6,\ u_i \neq v_i),表示存在一条连接城市 uiu_i 和 viv_i 的道路,其“难度”为 wiw_i。

下一行包含一个长度为 nn 的二进制字符串 ss,仅由字符 00 和 11 组成。若 si=1s_i = 1(1≤i≤n1 \le i \le n),则你可以在第 ii 个城市参加驾驶课程;若 si=0s_i = 0(1≤i≤n1 \le i \le n),则你不能在第 ii 个城市参加驾驶课程。

接下来一行包含一个整数 qq(1≤q≤1051 \le q \le 10^5),表示你需要回答的查询数量。

接下来的 qq 行,每行包含两个整数 aja_j、bjb_j(1≤aj,bj≤n, 1≤j≤q1 \le a_j, b_j \le n,\ 1 \le j \le q),表示第 jj 个查询中需要处理的两个城市。

保证所给图是一棵树。保证所有测试用例中 nn 的总和与 qq 的总和均不超过 10510^5。

输出格式

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 11 and 22, which is 11.

In the first query of the second test case, we can spend 33 time in city number 11 visiting the driving courses, then go to vertex 55. Then the minimum time required is 3+⌈52⌉+⌈102⌉=113 + \lceil\frac{5}{2}\rceil + \lceil\frac{10}{2}\rceil = 11.

在第一个测试用例的唯一查询中,最优策略是忽略驾驶课程。此时所需的最少时间为顶点 11 与顶点 22 之间的距离,即 11。

在第二个测试用例的第一个查询中,我们可在编号为 11 的城市花费 33 单位时间参加驾驶课程,然后前往顶点 55。此时所需的最少时间为 3+⌈52⌉+⌈102⌉=113 + \lceil\frac{5}{2}\rceil + \lceil\frac{10}{2}\rceil = 11。

输入解题思路,AI测评打分。不知道怎么写?

首页