Electric Car Rally
Time limit1sMemory limit128 MB
Plan driving and charging stops across time-dependent roads to reach the last station as early as possible.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
ElecCarCo wants to show that electric cars are practical, so it sponsors a cross country road rally. The course has charging stations numbered to , and a car can stop at any of them to charge its battery.
The rally can run over several days. A full battery holds minutes of driving, and every minute of driving costs two minutes plugged into a charger. Charge builds up in proportion to the time spent plugged in, so minutes on a charger buys minutes of driving, up to the limit of minutes. Every car leaves at noon on the first day with a full battery, and a car may stay at a station as long as it likes, even after the battery is full. A car cannot enter a road when the driving time left in its battery is less than the travel time of that road.
Roads exist only between certain pairs of stations. Traffic, road conditions, HOV lanes and similar factors make the travel time on a road depend on the time of day at which a car enters it. Every road is two way, and the same conditions apply in both directions.
The winner is the first car to reach station after starting from station . Apart from the start and the finish there are no restrictions: a car may pass through the other stations in any order, and it does not have to visit all of them.
Write a program that finds the earliest time a car can reach the final station, measured in minutes elapsed since the rally started.
Input
The input holds several test cases. Each test case begins with a line holding (), the number of stations, and (), the number of road segments.
Then come blocks, one per road segment. A block begins with a line holding two integers and (, ), the two stations joined by that segment. The segment is undirected, so a car can drive from to and from to .
After that come one to twenty travel lines. Each travel line holds three integers Start, Stop () and Time (). Start and Stop are times of day in minutes after midnight, and Time is the number of minutes the segment takes when a car enters it at any moment in . The first travel line of a block has Start equal to , and the last one has Stop equal to . The lines are given in increasing order of time and every line after the first starts one minute after the previous line stops, so the lines of a block cover the whole day from 00:00 to 23:59.
The input ends with a line holding two zeros. Every test case describes a course that a car can finish.
Output
For each test case, print the smallest number of minutes needed to finish the rally as a single integer on its own line. Print nothing else, and leave no blank line between answers.