Time Machine
Time limit1sMemory limit1024 MB
Find the minimum time to reach vertex N from vertex 1 when one edge a to b rewinds the clock by c, or report x if unreachable or -inf if the time can shrink forever.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, BFS
- Solved
- No attempts yet
Problem
A directed graph with vertices and edges is given. Moving along each edge takes unit of time. Vertex has a time machine. Using it moves you to vertex and sends the clock back by . There is no limit on how many times the time machine can be used. Starting at vertex at time , find the minimum arrival time at vertex . You may keep moving after you reach vertex .
Input
The first line contains , , , , and , separated by spaces.
From the second line to the -th line, line contains two integers and . This means the -th edge goes from to .
Output
Print the minimum arrival time on the first line. If it is impossible to reach vertex , print x. If the arrival time can be made arbitrarily small, print -inf.