This page is still under construction.

Parts of this page are still being built. What you see may change.

Electric Car Rally

Time limit1sMemory limit128 MB

Summary
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 nn charging stations numbered 00 to n−1n-1, and a car can stop at any of them to charge its battery.

The rally can run over several days. A full battery holds 240240 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 ww minutes on a charger buys w/2w/2 minutes of driving, up to the limit of 240240 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 n−1n-1 after starting from station 00. 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 nn (1≤n≤5001 \le n \le 500), the number of stations, and mm (1≤m≤10001 \le m \le 1000), the number of road segments.

Then come mm blocks, one per road segment. A block begins with a line holding two integers aa and bb (0≤a,b≤n−10 \le a, b \le n-1, a≠ba \ne b), the two stations joined by that segment. The segment is undirected, so a car can drive from aa to bb and from bb to aa.

After that come one to twenty travel lines. Each travel line holds three integers Start, Stop (0≤Start<Stop≤14390 \le \text{Start} < \text{Stop} \le 1439) and Time (0<Time<10000 < \text{Time} < 1000). 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 [Start,Stop][\text{Start}, \text{Stop}]. The first travel line of a block has Start equal to 00, and the last one has Stop equal to 14391439. 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.

Examples1

  1. Example 1

    Input
    4 4
    0 1
    0 1439 100
    0 2
    0 1439 75
    1 3
    0 720 150
    721 824 100
    825 1000 75
    1001 1439 150
    2 3
    0 1439 150
    3 2
    0 1
    0 10 200
    11 1439 300
    1 2
    0 10 200
    11 1439 300
    4 3
    0 1
    0 719 500
    720 1439 240
    1 2
    0 964 500
    965 1439 2
    2 3
    0 971 500
    972 1439 3
    0 0
    
    Expected output
    180
    2360
    255