eolymp
bolt
Try our new interface for solving problems
Məsələlər

Traffic Lights

Traffic Lights

Kenosha, the city nearest Farmer John, has \textbf{M} (\textbf{1} ≤ \textbf{M} ≤ \textbf{14,000}) roads conveniently numbered \textbf{1..M} that connect \textbf{N} (\textbf{2} ≤ \textbf{N} ≤ \textbf{300}) junctions which are conveniently numbered \textbf{1..N}. No two roads connect the same pair of junctions. No road connects a junction to itself. The integer travel time \textbf{T_ij} (\textbf{1} ≤ \textbf{T_ij} ≤ \textbf{100}) between junctions \textbf{i} and \textbf{j} is the same for both directions (i.e., \textbf{T_ij} = \textbf{T_ji}). Each junction has a single traffic light with two colors: blue or purple. The color of each light alternates periodically: blue for certain duration and then purple for another duration. Traffic is permitted to commence travel down the road between any two junctions, if and only if the lights at both junctions are the same color at the moment of departing from one junction for the other. The lights do not necessarily have to be the same on the whole trip down the road. If a vehicle arrives at a junction just at the moment the lights switch it must consider the new colors of lights. Vehicles are allowed to wait at the junctions. You are given the city map which shows: \begin{itemize} \item The travel times \textbf{T_ij} for all roads \item The durations of the two colors at junction \textbf{i}. (\textbf{DB_i} (1 ≤ \textbf{DB_i} ≤ \textbf{100}) for the blue light and \textbf{DP_i} (\textbf{1} ≤ \textbf{DP_i} ≤ \textbf{100}) for the purple light) \item The initial color \textbf{C_i} of the light at junction \textbf{i} (a letter '\textbf{B}' or '\textbf{P}' with the obvious meaning) and the remaining time \textbf{R_i} (\textbf{1} ≤ \textbf{R_i} ≤ \textbf{100}) for this color to change \end{itemize} Find the minimum time one needs to get from a given source \textbf{S} (\textbf{1} ≤ \textbf{S} ≤ \textbf{N}) to a given destination \textbf{D} (\textbf{1} ≤ \textbf{D} ≤ \textbf{N}; \textbf{D} ≠ \textbf{S}). Consider the map below with four junctions and five roads. FJ wants to travel from junction \textbf{1} to junction \textbf{4}. The first light is blue; the rest are purple. \includegraphics{https://static.e-olymp.com/content/58/58643215376a70d90f7f50ec98b073e01062aa69.jpg} The minimum time is \textbf{127} utilizing the path \textbf{1-2-4}. Initially, the light at junction 1 is blue. Since the light at junction \textbf{2} is purple, vehicle waits at junction \textbf{1} for \textbf{2} seconds then travels \textbf{4} seconds to junction \textbf{2}. At time \textbf{6}, the light at junction \textbf{2} switches to blue whereas the light at junction \textbf{4} has \textbf{32} more seconds to switch to blue. However, after \textbf{32} seconds, the light at junction \textbf{2} switches to purple and the light at junction \textbf{4} switches to blue at the same time. So the vehicle needs to wait \textbf{13} seconds more for junction \textbf{2} to switch to blue then the lights have the same color and vehicle travels \textbf{76} seconds to the destination junction \textbf{4}. The total time is \textbf{2+4+32+13+76=127} seconds. Below is a more graphical presentation of this travel plan: \includegraphics{https://static.e-olymp.com/content/a9/a95e34703702e8b977701e105a31145a0d611523.jpg} \InputFile Line \textbf{1}: Two space-separated integers: \textbf{S} and \textbf{D}. Line \textbf{2}: Two space-separated integers: \textbf{N} and \textbf{M}. Lines \textbf{3..N+2}: Line \textbf{i+2} line describes junction \textbf{i} with a character and three integers (all separated by a single space): \textbf{C_i}, \textbf{R_i}, \textbf{DB_i}, and \textbf{DP_i}. Lines \textbf{N+3..N+M+2}: Line \textbf{N+2+k} describes road \textbf{k} with three integers: \textbf{i}, \textbf{j}, and \textbf{T_ij}. \OutputFile Line \textbf{1}: One integer: the time taken by a minimum-time path from the source junction to the destination junction. If there is no path, output \textbf{0}.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 128 MiB
Giriş verilənləri #1
1 4
4 5
B 2 16 99
P 6 32 13
P 2 87 4
P 38 96 49
1 2 4
1 3 40
2 3 75
2 4 76
3 4 77
Çıxış verilənləri #1
127