//Lore
AQT: Hey Imax, I've been thinking about something …
IMAX: Hmmmm?
AQT: You ever thought about going back in time? Fixing some of your old mistakes?
IMAX: Well … I've always thought that it's good to let fate take its course … except for that one time… that time ruined my life.
AQT: Wanna tell me about it?
IMAX: Let's fix it instead.
This problem takes place in space-time. Space-time consists of two parts - Space and Time. Time flows continuously and can be represented by a single integer. Time flows forward, and after some waiting, any point at time
To summarize, at any point in space-time, imaxblue has 3 actions available:
Travel along an edge in space, consuming
fuel.Travel back in time, available iff there is a portal active in his current point in space-time, consuming
fuel.Wait for time to pass in his current node, travelling FORWARD in time and staying in the same location (using 0 fuel).
Imax starts at point
Imax gives you
If it is impossible, output -1
.
Constraints
Input Specification
Line 1:
Next
Next
Next
Output Specification
Sample Input 1
9 3 5 5
0 2 1
2 6 3
6 7 2
0 3 5
3 4 3
3 5 8
5 8 1
5 1 2
2 5 3 2
6 4 2 4
1 5 1 1
5 7
2 6
1 7
2 4
2 8
Sample Output 1
6
10
37
22
19
Sample Input 2
4 0 1 1
0 1 1000000000
1 2 1000000000
2 3 1000000000
1 3
Sample Output 2
3000000000
//lore
AQT: So, what was that bad decision you made?
IMAX: Setting that damn data structures problem on triway cup summer 2019.
Comments