#P6573. [BalticOI 2017] Toll
[BalticOI 2017] Toll
Background
As a qualified freight company, you need to deliver goods while spending as little money as possible.
Problem Description
This city has locations, and there are edges between these locations.
The freight company has received orders, and they need to minimize the money spent along the route.
For each road, there are three pieces of information:
- mean the road connects from to ;
- means how much money this road costs.
For each order, and are given, meaning you need to transport goods from to . For each order, find the minimum money needed; if it cannot be delivered, output .
In particular, for a road with endpoints numbered , it is guaranteed that:
$$\left\lfloor\dfrac{b}{k}\right\rfloor=1+\left\lfloor\dfrac{a}{k}\right\rfloor$$( is a given constant).
Input Format
The first line contains four integers , representing a constant, the number of locations, the number of edges, and the number of orders.
The locations are numbered from to .
The next lines each contain three integers , meaning that it costs to go from to . It is guaranteed that $\left\lfloor\dfrac{b}{k}\right\rfloor=1+\left\lfloor\dfrac{a}{k}\right\rfloor$, and between any two locations there is at most edge.
The next lines each contain two integers , meaning you need to transport goods from to .
Output Format
For each order, output one integer per line representing the answer.
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
15
9
7
8
-1
Hint
Constraints
This problem uses bundled testdata.
- Subtask 1 (7 pts): .
- Subtask 2 (10 pts): For every order, .
- Subtask 3 (8 pts): .
- Subtask 4 (31 pts): .
- Subtask 5 (44 pts): No special constraints.
For of the testdata, , , , , .
Notes
Translated from BOI 2017 D1 T3 Toll.
Translator: @一只书虫仔。
This problem enforces optimization.
Translated by ChatGPT 5