Backpack Buddies

시간 제한1초메모리 제한512 MB

요약
0번 오두막에서 n-1번 오두막까지 이동하는 최소 시간과 하루에 12시간까지만 걷는 조건에서의 최소 시간을 각각 구해 그 차이를 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 동적 계획법, 이분 탐색
정답자
아직 제출이 없습니다

문제

The prudent Mr. Day and 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; such trails all start and end at some cabin where it is possible to lay over. Mr. Day for one likes sleeping comfortably, so he immediately suggested that they sleep in a cabin every night.

However, Dr. Knight was of a different opinion; she argued that they’d arrive at their destination much quicker if they went for as long as possible each day, and then slept in tents in between two cabins if need be. Even though Mr. Day agreed that reaching the destination as quickly as possible was desirable, he was not willing to sacrifice the comfort of a proper bed.

After a fierce argument, Mr. Day and Dr. Knight decided to split up, each to follow their own strategy. Assuming they both walk at the same pace, how long will Dr. Knight need to wait at the final destination before Mr. Day shows up?

입력

The first line of input contains two integers, n (1 ≤ n ≤ 100 000) the number of cabins, and m (1 ≤ m ≤ 100 000) the number of trails. Then follows m lines, each describing a trail. The i’th such line contains three integers, ui, vi and di (0 ≤ ui, vi < n, 0 ≤ di ≤ 12) meaning that there is a trail between cabin ui and cabin vi which will take exactly d hours to traverse. (There can be more than one trail between two cabins, and some trails could lead back to the same cabin from where it started).

Mr. Day and Dr. Knight start in cabin 0 and their destination is cabin n − 1. Each day they start walking at 08:00 in the morning, and then walks 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.

예제2

  1. 예제 1

    입력
    5 6
    0 1 2
    0 3 8
    1 2 11
    2 3 5
    2 4 2
    4 3 9
    
    예상 출력
    4
    
  2. 예제 2

    입력
    3 2
    0 1 2
    1 2 12
    
    예상 출력
    10