Under construction

Still being built. Things may change

Traffic Lights

Time limit1sMemory limit128 MB

Summary
Find the earliest arrival time from a source to a destination on a weighted graph where each junction has a two-color periodic light and you may cross an edge only when both endpoint lights match.
Level

Medium7 of 10

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

Problem

The city of Kenosha has MM roads, numbered 1…M1 \ldots M, that connect NN junctions, numbered 1…N1 \ldots N. No two roads connect the same pair of junctions, and no road connects a junction to itself. The integer travel time TijT_{ij} between junctions ii and jj is the same in both directions, so Tij=TjiT_{ij} = T_{ji}.

Each junction has a single traffic light that shows one of two colors, blue or purple. Each light alternates periodically: it stays blue for a fixed number of seconds, then purple for another fixed number of seconds, then repeats. You may set off down the road between two junctions only if, at the exact instant you leave one junction for the other, the lights at both junctions show the same color. The two lights do not have to stay the same color for the rest of the trip.

If a vehicle reaches a junction at the exact instant its light switches, the new color applies. Vehicles may wait at a junction as long as they like. For each junction ii you are given its blue duration DBiDB_i, its purple duration DPiDP_i, its initial color CiC_i (B for blue or P for purple), and the remaining time RiR_i before that initial color first changes.

Starting at time 00 at a source junction SS, find the minimum time needed to reach a destination junction DD (D≠SD \ne S).

Constraints: 2≤N≤3002 \le N \le 300, 1≤M≤14,0001 \le M \le 14{,}000, 1≤Tij≤1001 \le T_{ij} \le 100, 1≤DBi≤1001 \le DB_i \le 100, 1≤DPi≤1001 \le DP_i \le 100, 1≤Ri≤1001 \le R_i \le 100, 1≤S,D≤N1 \le S, D \le N.

Worked example. Consider four junctions and five roads, where a vehicle wants to travel from junction 1 to junction 4.

JunctionInitial colorRemaining timeBlue durationPurple duration
1B21699
2P63213
3P2874
4P389649

Roads (with travel times): 1-2 (4), 1-3 (40), 2-3 (75), 2-4 (76), 3-4 (77).

The minimum time is 127, along the path 1 -> 2 -> 4:

  • Junction 1 starts blue while junction 2 is purple, so the vehicle waits 2 seconds until junction 1 also turns purple, then drives 4 seconds to junction 2, arriving at time 6.
  • At time 6 junction 2 turns blue, but junction 4 stays purple for 32 more seconds. After those 32 seconds junction 2 turns purple exactly as junction 4 turns blue, so they still differ. The vehicle waits 13 more seconds until junction 2 turns blue; now both are blue, so it drives 76 seconds to junction 4.
  • Total time: 2+4+32+13+76=1272 + 4 + 32 + 13 + 76 = 127 seconds.

Input

  • Line 1: two space-separated integers SS and DD.
  • Line 2: two space-separated integers NN and MM.
  • Lines 3 to N+2N+2: line i+2i+2 describes junction ii with one character and three integers separated by single spaces: CiC_i, RiR_i, DBiDB_i, and DPiDP_i.
  • Lines N+3N+3 to N+M+2N+M+2: line N+2+kN+2+k describes road kk with three integers ii, jj, and TijT_{ij}.

Output

  • Line 1: a single integer, the time of a minimum-time path from the source junction to the destination junction. If no path exists, output 00.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    127
    
  2. Example 2

    Input
    1 2
    2 1
    B 5 10 10
    B 5 10 10
    1 2 7
    
    Expected output
    7