Under construction

Still being built. Things may change

Trip the Lights Fantastic

Time limit1sMemory limit128 MB

Summary
Find the fastest car route through a town whose traffic lights cycle through green, yellow and red, where arriving on red costs a 5 second stop.
Level

Medium7 of 10

Topics
Graph, Shortest path, Simulation, Implementation
Solved
No attempts yet

Problem

Bob works at the Traffic Commission for a medium-size town. He is in charge of monitoring the traffic lights in the city and dispatching repair crews when necessary. With a lot of free time, he tries to figure out the quickest way to make short trips between various points in the city. Bob knows the layout of the streets and the location and cycle times of every traffic light. To simplify things, he makes these assumptions:

  1. All cars travel at the same top speed, and a car sitting at a red light takes 5 seconds to react and get up to speed. (That is, the car is essentially standing still for 5 seconds, then proceeds at top speed. Bob also assumes the light will not turn back to red during those 5 seconds.)
  2. Each car approaches a light at full speed and either passes through it if it is green or yellow, or comes to an immediate stop if it is red. A car may pass through a light if it arrives exactly as the light turns green. A car must stop if it reaches the light exactly as the light turns red.
  3. The time to make turns through a light is ignored. It is possible to travel between any two lights, though perhaps not directly.

Furthermore, no U-turns are allowed, and a route never revisits an intersection. Help Bob find minimum-time paths.

A light with green, yellow, and red durations gg, yy, rr has cycle length g+y+rg + y + r. All lights begin their green period at time 0; during a cycle, seconds [0,g)[0, g) are green, [g,g+y)[g, g+y) are yellow, and [g+y,g+y+r)[g+y, g+y+r) are red, and then the cycle repeats.

Input

The input contains several test cases. The first line of each test case has four positive integers nn, mm, ss, and ee, where nn (2≤n≤1002 \le n \le 100) is the number of traffic lights (numbered 00 through n−1n - 1), mm is the number of roads between the lights, and ss and ee (s≠es \ne e) are the start and end lights of the desired trip.

The next nn lines each have the form g y r, giving the number of seconds each light is green, then yellow, then red (1≤g,y,r≤1001 \le g, y, r \le 100). The first of these lines is for light 00, the second for light 11, and so on.

The following mm lines each describe one road, in the form l1 l2 t, where l1 and l2 are the two lights the road connects and tt is the time in seconds to travel the road at full speed (t≤500t \le 500); add 55 to this value to obtain the travel time when starting the road from a standstill. All roads are two-way.

At time 00 all lights are just starting their green period and your car is at a standstill at light ss. Because it takes 5 seconds to get going, you may assume g+yg + y is never less than or equal to 55. The last test case is followed by a line containing 0 0 0 0, which ends the input.

Output

For each test case, output a single line with the minimum time to travel from the start light to the end light, in the form mm:ss (minutes and seconds). If the number of seconds is less than 10, pad it with a leading zero (for example, output 4:05, not 4:5). If the number of minutes is less than 10, print just one digit (as in 4:05).

Examples1

  1. Example 1

    Input
    3 3 0 2
    3 4 5
    3 3 3
    2 4 4
    0 1 1
    1 2 2
    0 2 12
    3 3 0 2
    3 4 5
    3 4 3
    2 4 4
    0 1 1
    1 2 2
    0 2 12
    0 0 0 0
    
    Expected output
    0:16
    0:08