luogu#P9618. 地铁
地铁
Background
A second-year student, all alone.
Looking up at the sky above, the gray city’s dome.
In this dating room called the universe,
Maybe we just happened to not meet.
Problem Description
A famous engineering expert, 625OutContradiction, designed a subway transportation network . has stations and subway lines.
The -th subway line passes through several stations in the transportation network, in the form . Between every two adjacent stations , there is a one-way subway track belonging to line that goes from to . It is guaranteed that a subway line does not pass through the same station more than once. However, a station may be passed through by multiple subway lines.
Danyu and Eriou are going from station to station . However, their bicycle broke, so they have to take the subway. Now they need to decide a travel plan.
A travel plan works like this: starting from station , choose a subway line that passes through station and start riding. While riding along the current subway line, you may choose to transfer to any other subway line that passes through the current station. You must finally reach station . During the ride, it is allowed to visit a station or a subway track multiple times.
Note: when departing from station , the first time you take the subway is not counted as a transfer.
Eriou asks questions. For each question, Eriou provides three parameters . In this question, if a travel plan passes through subway tracks and makes transfers, then its fatigue value is . You need to answer: among travel plans with no more than transfers, what is the minimum fatigue value.
Input Format
The first line contains three integers .
The next lines describe the subway lines. The -th line is in the form: , representing one subway line.
The next lines each contain three integers, representing .
Output Format
Output lines, each corresponding to the answer for one question.
5 3 3
5 1 2 3 4 5
2 1 3
3 2 4 5
1 1 1
3 0 2
1 5 2
4
9
4
10 7 10
10 1 2 3 4 5 6 7 8 9 10
5 3 8 5 1 6
2 1 6
4 3 7 8 5
1 1
2 10 2
6 8 4 7 3 1 5
5 10 6
17 14 0
11 14 5
8 8 3
8 13 9
11 2 9
7 1 6
11 11 8
15 3 0
0 17 4
35
153
69
48
53
57
36
66
135
0
10 7 10
10 1 2 3 4 5 6 7 8 9 10
3 2 7 1
3 5 10 9
2 2 7
5 4 8 1 7 2
3 10 9 4
4 2 1 7 8
18 6 0
16 11 0
18 1 0
14 0 0
19 14 0
3 2 0
18 15 0
5 18 0
2 17 0
20 10 0
162
144
162
126
171
27
162
45
18
180
Hint
Sample #1 Explanation
$1\rightarrow 2\rightarrow 3\rightarrow 4\rightarrow 5$ is the first given subway line. and are the second and third subway lines.
For the first two queries, there is an optimal travel plan: take the second subway line from station to station , then transfer at station to the first subway line to reach the destination. In total, it passes through subway tracks and makes transfer, so the answers for the first two queries are and , respectively. For the third query, since transfers are expensive, the optimal plan is to follow the first subway line all the way to the destination, passing through subway tracks, so the answer is .
Constraints
For all data:
, , , .
, .
For of the data: , , .
For another of the data: .
For another of the data: .
There may be subway lines in the problem that pass through only one station. Such lines can be ignored directly. The testdata guarantees that for any query, there exists a valid route that can reach the destination.
Translated by ChatGPT 5