Jakarta Traffic Jam
InterviewTime limit1sMemory limit128 MB
Find the fastest travel time between two intersections where each street is driven at half speed during its own daily rush hour, and you never stop to wait. Each test case gives up to 20 nodes; report the minimum minutes to two decimals.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Simulation, Implementation
- Solved
- No attempts yet
Problem
Traffic in Jakarta is terrible. During rush hour the streets get congested, and while a street is congested you can only drive at half of your normal speed. So you must plan your trip carefully if you do not want to be late.
For example, suppose a street from P to Q normally takes minutes to drive, and it is congested from 15:00 until 16:00. Consider a few situations:
- If you leave P minutes before rush hour starts (at 14:45), you cover minutes' worth of distance at normal speed before the congestion begins. The remaining distance is worth normal minutes, but at half speed it takes minutes to cover. So the whole trip from P to Q takes minutes.
- If you leave during the last minutes of rush hour (at 15:55), the distance you cover in those first congested minutes equals minutes' worth at normal speed. The remaining distance is worth normal minutes, and since rush hour is now over it takes more minutes. So the trip takes minutes.
- Other combinations of the above are possible.
In other words, whenever you are on a street during its rush hour you move at half speed, and otherwise at normal speed. Each street's rush hour is defined within a single day and never rolls over past midnight.
Given a map with at most intersections and streets, find the minimum time needed to travel from to , in minutes. Assume you drive continuously without stopping to wait.
Input
The input contains several test cases.
Each test case begins with a line containing two integers () and , the number of intersections and the number of streets. Intersections are numbered starting from .
Each of the next lines describes one street. A line starts with three integers , , and (), meaning there is a street connecting intersection and intersection that takes minutes to drive at normal speed (streets are two-way). These are followed by the character N or R. N has nothing after it and means the street has no rush hour. R is followed by the start and end time of the rush hour in hh:mm format (from 00:00 to 23:59). No rush hour rolls over past midnight.
The last line of each test case contains the starting point , the destination , and the current time in hh:mm format.
The input is terminated by a line containing two zeros, which must not be processed.
Output
For each test case, output on its own line the minimum time in minutes needed to go from to . Always print exactly two digits after the decimal point.