#P16987. [NWERC 2018] Date Pickup

[NWERC 2018] Date Pickup

Problem Description

Richard and Janet are going on their first date. Richard has offered to meet her at home with his bicycle, and Janet tells him she will call when she is ready in 1010 to 2020 minutes. But Richard is impatient; while he could wait at home, he might also leave early and travel around the neighbourhood to minimise the time it takes him to reach her once she calls.

Given the directed graph representing the neighbourhood around Richard's and Janet's houses, Richard wants to devise a route around the neighbourhood, after an optional waiting period at his own house, which minimises the time that Janet has to wait in the worst case. He can travel for as long as he likes and visit each intersection as many times as he likes.

Janet will call Richard as soon as she is ready, and at that point Richard will take the shortest path to her that he can. Richard does not know exactly when Janet will be ready, but he knows it will be somewhere between aa and bb minutes, not necessarily at a whole minute.

If Richard is passing through an intersection at the exact same instant Janet calls, the call is considered to happen before he chooses what to do at the intersection. It could happen that Janet never has to wait for ww minutes, but might have to wait for wϵw-\epsilon minutes for arbitrarily small ϵ>0\epsilon>0; in this case, the worst case waiting time is still defined to be ww.

Input Format

The input consists of:

  • One line with two integers a,ba,b (0ab10120\le a\le b\le 10^{12}), indicating that Janet will be ready in at least aa minutes and at most bb minutes.
  • One line with two integers n,mn,m (2nm1052\le n\le m\le 10^5), the number of intersections and roads.
  • mm lines, each with three integers u,v,tu,v,t (1u,vn1\le u,v\le n, 1t1061\le t\le 10^6), indicating a one-way road from uu to vv taking exactly tt minutes.

Richard's house is at intersection 11 and Janet's house is at intersection nn. It is guaranteed that it is possible to travel from Richard's house to Janet's house, and that every intersection has at least one outgoing road.

Output Format

Output the time Janet has to wait in the worst case assuming she will be ready in at least aa minutes and at most bb minutes and Richard plans his route optimally. It can be shown that the worst case waiting time is always an integer.

10 20
3 5
1 3 7
2 1 1
2 3 2
2 3 5
3 2 4
6
4 10
5 7
1 4 6
4 5 5
4 5 3
5 5 30
1 2 1
2 3 1
3 2 1
5