#P9329. [JOISC 2023 Day1] Two Currencies

[JOISC 2023 Day1] Two Currencies

题目描述

There are NN cities in JOI Kingdom, numbered from 11 to NN. There are N1N - 1 roads in JOI Kingdom, numbered from 11 to N1N - 1. The road i (1iN1)i \ (1 \le i \le N - 1) connects the city AiA_i and the city BiB_i bi-directionally. It is possible to travel from any city to any other city by passing through some of the roads.

There are checkpoints on some of the roads in JOI Kingdom. There are MM checkpoints, numbered from 11 to MM. The checkpoint j (1jM)j \ (1 \le j \le M) is located on the road PjP_j. In order to pass through it, you need to pay either one gold coin or CjC_j silver coins.

There are QQ citizens in JOI Kingdom, numbered from 11 to QQ. The citizen k (1kQ)k \ (1 \le k \le Q) has XkX_k gold coins and YkY_k silver coins, and wants to travel from the city SkS_k to the city TkT_k. Since gold coins are valuable, all the citizens want to keep as many gold coins as possible.

Write a program which, given information of the cities, the roads, the checkpoints, and the citizens in JOI Kingdom, for each k (1kQ)k \ (1 \le k \le Q), determines whether it is possible for the citizen kk to travel from the city SkS_k to the city TkT_k, and, if it is possible, calculates the maximum possible number of gold coins the citizen kk can keep.

输入格式

Read the following data from the standard input.

N M QN \ M \ Q

A1 B1A_1 \ B_1

A2 B2A_2 \ B_2

\vdots

AN1 BN1A_{N - 1} \ B_{N - 1}

P1 C1P_1 \ C_1

P2 C2P_2 \ C_2

\vdots

PM CMP_M \ C_M

S1 T1 X1 Y1S_1 \ T_1 \ X_1 \ Y_1

S2 T2 X2 Y2S_2 \ T_2 \ X_2 \ Y_2

\vdots

SQ TQ XQ YQS_Q \ T_Q \ X_Q \ Y_Q

输出格式

Write QQ lines to the standard output. In the kk-th line (1kQ)(1 \le k \le Q), if the citizen kk can travel from the city SkS_k to the city TkT_k, output the maximum possible number of gold coins the citizen kk can keep. Otherwise, output 1-1 in the kk-th line.

题目大意

题目描述

在 JOI 王国中,有 nn 个城市,编号从 11nn。JOI 王国有 n1n−1 条双向道路,编号从 11n1n−1。第 ii 条道路连接城市 aia_i 和城市 bib_i

在 JOI 王国中,一些道路上放有检查站。有 mm 个检查站,编号从 11mm。第 jj 个检查站位于道路 pjp_j 上。通过该检查站需要支付 11 枚金币或 cjc_j 枚银币。

在 JOI 王国有 qq 名公民,编号从 11qq。第 kk 名公民持有 xkx_k 枚金币和 yky_k 枚银币,并希望从城市 sks_k 前往城市 tkt_k。由于金币具有较高的价值,所有公民都希望尽可能多地保留金币。

编写一个程序,给定 JOI 王国中的城市、道路、检查站和公民信息,对于每个 k(1kq)k (1≤k≤q),判断公民 kk 是否能够从城市 sks_k 前往城市 tkt_k,并在此条件成立时计算公民 kk 所能保留的最多金币数。

输入格式

从标准输入读入以下数据。

N M QN \ M \ Q

A1 B1A_1 \ B_1

A2 B2A_2 \ B_2

\vdots

AN1 BN1A_{N - 1} \ B_{N - 1}

P1 C1P_1 \ C_1

P2 C2P_2 \ C_2

\vdots

PM CMP_M \ C_M

S1 T1 X1 Y1S_1 \ T_1 \ X_1 \ Y_1

S2 T2 X2 Y2S_2 \ T_2 \ X_2 \ Y_2

\vdots

SQ TQ XQ YQS_Q \ T_Q \ X_Q \ Y_Q

输出格式

向标准输出打印 qq 行。在第 kk(1kq)(1≤k≤q) 中,如果公民 kk 可以从城市 sks_k 前往城市 tkt_k,请输出公民 kk 可以保留的最多金币数。否则,在第 kk 行中输出 1−1

Translate by

https://www.luogu.com.cn/user/661274

5 4 3
1 2
1 3
2 4
2 5
2 9
2 4
3 5
4 7
3 4 2 11
5 3 4 5
2 3 1 1

1
2
-1

10 7 9
1 8
6 3
5 9
7 9
3 1
3 4
10 1
2 6
5 6
9 4
7 4
7 4
2 4
7 4
7 4
1 4
8 6 5 3
3 9 8 0
4 7 6 15
7 4 9 3
6 4 8 0
9 10 5 16
5 3 2 4
2 8 4 3
6 1 3 3

3
6
6
7
7
3
1
2
2

8 7 11
1 2
2 3
3 4
4 5
5 6
6 7
7 8
4 4
3 7
2 10
5 2
4 1
4 4
5 6
6 3 7 69
7 1 5 55
3 1 6 8
8 2 5 45
4 6 4 45
6 1 3 33
2 1 0 19
3 7 2 31
7 1 2 31
7 2 4 58
8 3 5 63

7
5
5
5
4
2
0
2
1
4
5

8 7 11
1 8
1 4
3 1
3 6
6 7
2 1
5 2
5 5
5 8
4 7
6 6
4 1
6 4
1 7
4 7 2 18
2 4 5 1
4 2 1 32
1 5 7 21
2 5 0 50
8 4 4 33
1 7 6 16
4 8 7 18
1 2 8 13
5 4 10 42
7 1 6 40

1
3
1
7
0
4
5
7
8
10
6

提示

【样例解释 #1】

The citizen 11 can travel from the city 33 to the city 44 as follows. After the travel, the citizen 11 keeps one gold coin.

  1. The citizen 11 travels from the city 33 to the city 11 by passing through the road 22. There are the checkpoints 1,21, 2 on the road 22. The citizen 11 pays one gold coin at the checkpoint 11 and passes through it, and 44 silver coins at the checkpoint 22 and passes through it, respectively. After that, the citizen 11 keeps one gold coin and 77 silver coins.
  2. The citizen 11 travels from the city 11 to the city 22 by passing through the road 11. Since there is no checkpoint on the road 11, the citizen 11 keeps one gold coin and 77 silver coins.
  3. The citizen 11 travels from the city 22 to the city 44 by passing through the road 33. There is the checkpoint 33 on the road 33. The citizen 11 pays 55 silver coins at the checkpoint 33 and passes through it. After that, the citizen 11 keeps one gold coin and 22 silver coins.

Since it is impossible for the citizen 11 to travel by finally keeping more than or equal to 22 gold coins, output 11 in the first line.

The citizen 22 can travel from the city 55 to the city 33 as follows. After the travel, the citizen 22 keeps two gold coins.

  1. The citizen 22 travels from the city 55 to the city 22 by passing through the road 44. There is the checkpoint 44 on the road 44. The citizen 22 pays one gold coin at the checkpoint 44 and passes through it. After that, the citizen 22 keeps 33 gold coins and 55 silver coins.
  2. The citizen 22 travels from the city 22 to the city 11 by passing through the road 11. Since there is no checkpoint on the road 11, the citizen 22 keeps 33 gold coins and 55 silver coins.
  3. The citizen 22 travels from the city 11 to the city 33 by passing through the road 22. On the road 22, there are the checkpoints 1,21, 2. The citizen 22 pays one gold coin at the checkpoint 11 and passes through it, and 44 silver coins at the checkpoint 22 and passes through it, respectively. After that, the citizen 22 keeps 22 gold coins and one silver coin.

Since it is impossible for the citizen 22 to travel by finally keeping more than or equal to 33 gold coins, output 22 in the second line.

Since it is impossible for the citizen 33 to travel from the city 22 to the city 33, output 1-1 in the third line.

该样例满足子任务 1,41, 4 的限制。

【样例解释 #2】

该样例满足子任务 1,2,41, 2, 4 的限制。

【样例解释 #3】

该样例满足子任务 1,3,41, 3, 4 的限制。

【样例解释 #4】

该样例满足子任务 1,41, 4 的限制。

【数据范围】

对于所有测试数据,满足 2N1052 \le N \le 10 ^ 51M,Q1051 \le M, Q \le 10 ^ 51Ai,BiN1 \le A_i, B_i \le N1PjN11 \le P_j \le N - 11Cj1091 \le C_j \le 10 ^ 91Sk,TkN1 \le S_k, T_k \le NSkTkS_k \ne T_k0Xk1090 \le X_k \le 10 ^ 90Yk10180 \le Y_k \le 10 ^ {18},保证给定的道路使所有城市连通,保证所有输入均为整数。

子任务编号 分值 限制
11 1010 N,M,Q2000N, M, Q \le 2000
22 2828 C1=C2==CMC_1 = C_2 = \dots = C_M
33 3030 Ai=iA_i = iBi=i+1B_i = i + 1
44 3232