#LCA0000098. Phí qua đường (Toll)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phí qua đường (Toll)
Toll
Source: Baltic Olympiad in Informatics
Version: Phuoc Hung OJ Extended
Problem Statement
A delivery company wants to transport goods between locations. Every road is one-way and has a positive toll. For each order, find the minimum sum of tolls along a directed route from the departure location to the destination, or print -1 if no route exists.
Locations are numbered from to . An integer divides the locations into consecutive layers, where the layer of location is . Every directed edge goes to exactly the next layer, satisfying
$$\left\lfloor\frac{b}{k}\right\rfloor=\left\lfloor\frac{a}{k}\right\rfloor+1.$$Find the minimum toll for each order.
Input
The first line contains four integers . The next lines each contain , a directed edge with toll . The final lines each contain , an order from location to .
Output
For every order, print its minimum toll on a separate line, or -1 if no directed path exists.
Subtasks
- Subtask 1 (20 points): ; ; ; trong mỗi đơn hàng; ; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. không vượt quá số cạnh hợp lệ có thể có giữa các tầng. Additionally, , .
- Subtask 2 (30 points): ; ; ; trong mỗi đơn hàng; ; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. không vượt quá số cạnh hợp lệ có thể có giữa các tầng. Additionally, , .
- Subtask 3 (50 points): ; ; ; trong mỗi đơn hàng; ; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. không vượt quá số cạnh hợp lệ có thể có giữa các tầng.
Examples
Input
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
Output
15
9
7
8
-1
Explanation
The cheapest route from to is with total toll , rather than costing . The three single-edge routes cost . Location is unreachable, so the final answer is -1.