Bus Trip
Time limit1sMemory limit128 MB
Find a bus route plan from town 1 to town P arriving by time T that guarantees valid connections while minimizing worst-case total waiting time.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
There are towns and one-way direct bus routes (with no intermediate stops) connecting them. The towns are numbered from to . A traveler is in town at time and must reach town . Someone will meet him at the bus station in town at exactly time ; if he arrives earlier, he has to wait.
For each bus route we know its source town and destination town . We also know its departure and arrival times, but only approximately: the bus departs from at some moment in the range and arrives at at some moment in the range (both endpoints included).
The traveler dislikes waiting, so he wants a travel plan that minimizes the maximum possible total waiting time while still guaranteeing that he never misses a connection. A connection is guaranteed only if, whenever he changes buses, the latest possible arrival time of the incoming bus is no later than the earliest possible departure time of the outgoing bus.
When computing waiting time, always assume the earliest possible arrival time and the latest possible departure time.
Write a program that finds a suitable plan for the traveler.
Input
The first line contains four integers (), (), (), and ().
Each of the next lines describes one bus route with six integers , , , , , , where and are the source and destination towns and describe the departure and arrival ranges as explained above (, , ).
Output
Print a single line with the maximum possible total waiting time of the best travel plan. If it is impossible to guarantee arrival in town by time , print instead.