luogu#P8312. [COCI 2021/2022 #4] Autobus
[COCI 2021/2022 #4] Autobus
Problem Description
In a country, there are cities. These cities are connected by bus routes. The -th route starts from city and stops at city , and it takes minutes along the way.
Ema likes traveling, but she does not like transferring between bus routes. During a trip, she wants to take at most different bus routes.
Ema wants to know the shortest travel time from city to city (using at most different bus routes).
Input Format
The first line contains two integers , representing the number of cities and the number of bus routes.
The next lines describe the routes. The -th line contains three integers , representing the start city, the end city, and the time needed from the start to the end for the -th bus route.
The next line contains two integers , representing the maximum number of different bus routes that can be taken, and the number of queries.
The next lines describe the queries. The -th line contains two integers , asking for the shortest travel time from city to city .
Output Format
Output lines. The -th line contains one integer, the shortest travel time from city to city .
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
10
-1
0
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
6
4
0
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
3
4
0
Hint
Sample Explanation.

In each sample, the answers have been marked in the figure.
Constraints.
This problem uses bundled subtasks.
- Subtask 1 (15 pts): .
- Subtask 2 (15 pts): .
- Subtask 3 (25 pts): .
- Subtask 4 (15 pts): No additional constraints.
For of the testdata, , , , , and .
Hints and Notes.
The score setting follows the original COCI problem, with a full score of .
Translated from COCI2021-2022 CONTEST #4 T2 Autobus.
Translated by ChatGPT 5