#P9549. 「PHOI-1」路虽远
「PHOI-1」路虽远
Background
. Testdata has been fixed and strengthened; you can resubmit your code.
The road is long, but if you keep going, you will arrive.

Problem Description
Please note the special time limit of this problem.
Little X arrives in City Z. City Z has intersections and roads. The -th road connects intersection and intersection (you can travel from to and also from to ). Little X needs seconds to pass this road; if there is a speed limit, then passing this road takes seconds.
Now, the mayor will add speed limits to roads. However, which roads will have speed limits is decided by Little X.
At the same time, the mayor will install traffic lights at every intersection. The traffic light at intersection shows green for seconds first, then yellow for seconds, then red for seconds, and then repeats green, yellow, red, and so on. If the traffic light at an intersection is not red, Little X can depart from this intersection to another intersection. If the light color changes at the exact moment he arrives at an intersection, the color after the change is used. The durations of yellow and red may be . Also, Little X can run a yellow light at most times.
After a while, the mayor suddenly finds that at some moment, all traffic lights turn green. At the same time, Little X starts from intersection and heads to intersection . He wants to know the minimum time needed to reach intersection .
Because the road is long, but if you keep going, you will arrive. The testdata guarantees that reaching is always possible, i.e., intersections are connected.
Input Format
The first line contains integers .
The next lines each contain integers .
The next lines each contain integers .
Output Format
One line: the minimum time Little X needs.
5 8 6 1
1 2 2
1 0 3
1 1 4
3 1 0
5 1 4
1 2 1 4
2 4 2 4
3 4 0 2
1 5 7 8
1 3 1 2
5 4 2 3
2 5 2 4
1 4 4 7
4
9 16 14 2
1 7 2
1 5 3
1 6 4
3 5 0
1 6 5
1 0 4
1 7 5
3 8 8
3 8 6
1 2 1 4
8 5 2 6
2 4 2 4
8 6 3 5
3 4 0 2
6 7 1 4
1 5 7 8
5 9 16 21
1 3 2 2
7 6 2 3
5 4 2 3
6 9 3 5
2 5 2 4
8 9 4 7
1 4 4 7
6 5 6 9
18
Hint
This problem uses bundled tests.
| Subtask | Score | |||
|---|---|---|---|---|
| No special restrictions | ||||
| No special restrictions | ||||
| No special restrictions | ||||
| No special restrictions | ||||
For of the data, it is guaranteed that , , , , , .
Sample Explanation #1:
Take the path , and add speed limits on the roads with indices . Run the yellow light when arriving at , and pass directly on green when arriving at . Then takes second, takes seconds, takes seconds, for a total of seconds.
Sample Explanation #2:
Take the path , and add speed limits on the roads with indices . Run the yellow light when arriving at , pass directly on green when arriving at , and wait second at a red light when arriving at . Then takes second, takes seconds, takes seconds, takes seconds, plus the second waiting for the red light, for a total of seconds.
Translated by ChatGPT 5