Fast Waiting
InterviewTime limit2sMemory limit1024 MB
Find the minimum time from stop 1 to stop N when Jaewon can make up to K routes depart immediately instead of waiting for their next scheduled bus.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Jaewon's town has bus stops and bus routes.
Route departs from stop and arrives at stop after hours. It does not stop at any other stop. Route has a dispatch interval : a bus leaves stop at hour 0, and then again every hours.
Jaewon is in a hurry, so he can use fast waiting. With fast waiting, he picks a route that departs from his current stop and makes it leave right now, regardless of its dispatch interval.
Find the minimum time for Jaewon to go from stop 1 to stop when he uses fast waiting at most times.
Input
The first line contains , , and , the number of stops, the number of routes, and the maximum number of fast waiting uses, separated by spaces.
Each of the next lines contains , , , and for route , separated by spaces.
Output
Print the minimum time to go from stop 1 to stop on the first line. If it is impossible, print .