#P9549. 「PHOI-1」路虽远

「PHOI-1」路虽远

Background

updateupdate onon 2023.8.172023.8.17 12:2512:25. 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 nn intersections and mm roads. The ii-th road connects intersection uiu_i and intersection viv_i (you can travel from uiu_i to viv_i and also from viv_i to uiu_i). Little X needs pip_i seconds to pass this road; if there is a speed limit, then passing this road takes qiq_i seconds.

Now, the mayor will add speed limits to kk roads. However, which kk 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 ii shows green for xix_i seconds first, then yellow for yiy_i seconds, then red for ziz_i 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 00. Also, Little X can run a yellow light at most gg 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 11 and heads to intersection nn. He wants to know the minimum time needed to reach intersection nn.

Because the road is long, but if you keep going, you will arrive. The testdata guarantees that reaching is always possible, i.e., intersections 1n1 \sim n are connected.

Input Format

The first line contains 44 integers n,m,k,gn,m,k,g.

The next nn lines each contain 33 integers xi,yi,zix_i,y_i,z_i.

The next mm lines each contain 44 integers ui,vi,pi,qiu_i,v_i,p_i,q_i.

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 n,mn,m yi,ziy_i,z_i k,gk,g Score
00 1n,m51 \le n,m \le 5 No special restrictions 2020
11 No special restrictions yi=zi=0y_i=z_i=0 k=g=0k=g=0 55
22 yi=0,0zi109y_i=0,0 \le z_i \le 10^9 No special restrictions 2525
33 No special restrictions 5050

For 100%100\% of the data, it is guaranteed that 1n,m1001 \le n,m \le 100, 0k,gm0 \le k,g \le m, 1xi1091 \le x_i \le 10^9, 0yi,zi1090 \le y_i,z_i \le 10^9, 1u,vn1 \le u,v \le n, 0piqi1090 \leq p_i \leq q_i \leq 10^9.

Sample Explanation #1:

Take the path 13451 \to 3 \to 4 \to 5, and add speed limits on the roads with indices 1,2,4,6,7,81,2,4,6,7,8. Run the yellow light when arriving at 33, and pass directly on green when arriving at 44. Then 131 \to 3 takes 11 second, 343 \to 4 takes 00 seconds, 454 \to 5 takes 33 seconds, for a total of 44 seconds.

Sample Explanation #2:

Take the path 125691 \to 2 \to 5 \to 6 \to 9, and add speed limits on the roads with indices 2,3,4,5,6,7,8,9,10,11,13,14,15,162,3,4,5,6,7,8,9,10,11,13,14,15,16. Run the yellow light when arriving at 2,52,5, pass directly on green when arriving at 44, and wait 11 second at a red light when arriving at 66. Then 121 \to 2 takes 11 second, 252 \to 5 takes 44 seconds, 565 \to 6 takes 99 seconds, 696 \to 9 takes 33 seconds, plus the 11 second waiting for the red light, for a total of 1818 seconds.

Translated by ChatGPT 5