Problem list
Log in
#coci00611 · COCI 2021/2022 Contest 4

Autobus

Gold I 1.0 s 512 MB

In a country there are $n$ cities. The cities are connected by $m$ bus routes, where the $i$-th route starts in city $a_{i}$, ends in city $b_{i}$ and takes $t_{i}$ minutes. Ema loves to travel, but doesn’t like transferring between buses. On her trip she wants to use at most $k$ different bus routes. Help her answer $q$ questions of the form ‘What is the shortest travel time to get from city $c_{j}$ to city $d_{j}$ (using at most $k$ different bus routes)?’.

Input

The first line contains two positive integers $n$ and $m$ ($2 \le n \le 70$, $1 \le m \le 10^{6}$), the number of cities and the number of bus routes. The $i$-th of the next $m$ lines contains positive integers $a_{i}$, $b_{i}$ and $t_{i}$ ($1 \le a_{i}$, $b_{i} \le n$, $1 \le t_{i} \le 10^{6}$), the terminal cities and the travel time of the $i$-th bus route. The next line contains two positive integers $k$ and $q$ ($1 \le k \le 10^{9}$, $1 \le q \le n^{2}$), the maximum number of used routes and the number of queries. The $j$-th of the next $q$ lines contains positive integers $c_{j}$ and $d_{j}$ ($1 \le c_{j}$, $d_{j} \le n$), the cities from the $j$-th query.

Output

Print $q$ lines. In the $j$-th line print the shortest travel time from the $j$-th query, $or -1$ if there is no trip that satisfies the requirements.

Example 1

Input
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
1 3
1 4
4 2
3 3
Output
10
-1
0

Example 2

Input
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
2 3
1 4
4 2
3 3
Output
6
4
0

Example 3

Input
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
3 3
1 4
4 2
3 3
Output
3
4
0

Clarification of the examples: The answer to the first query from each example is marked on the graph.

Source
COCI 2021/2022 Contest 4
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code