Backpack Buddies
Time limit1sMemory limit512 MB
Find how many hours Dr. Knight waits at cabin n-1 when Mr. Day must end each day at a cabin, while Dr. Knight can stop anywhere after at most 12 hours.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
문제
The prudent Mr. Day and the adventurous Dr. Knight have decided to be backpack buddies and go trekking in the gorgeous Norwegian mountains. Of course they will use the trails marked by the Norwegian Trekking Association (DNT), like any sane trekkers would. Every such trail starts and ends at a cabin where it is possible to stay overnight.
Mr. Day likes sleeping comfortably, so he immediately suggested that they sleep in a cabin every night.
Dr. Knight disagreed. She argued that they would arrive at their destination much sooner if they walked as far as possible each day, then slept in tents between two cabins when needed. Mr. Day agreed that reaching the destination quickly was desirable, but he was not willing to give up the comfort of a proper bed.
After a fierce argument, they decided to split up and each follow their own strategy. Assuming they walk at the same pace, how many hours will Dr. Knight need to wait at the final destination before Mr. Day shows up?
입력
The first line contains two integers, (), the number of cabins, and (), the number of trails. The next lines each describe a trail. The -th of these lines contains three integers , , and (, ), meaning there is a trail between cabins and that takes exactly hours to traverse. There can be more than one trail between two cabins, and a trail can lead back to the cabin it started from.
Mr. Day and Dr. Knight start in cabin 0, and their destination is cabin . Each day they start walking at 08:00 and walk non-stop for at most 12 hours before settling down for the night.
출력
Output a single integer, the number of hours Dr. Knight needs to wait at the destination before Mr. Day shows up.