Cheap Traveling
Time limit2sMemory limit512 MB
Find a route from village 1 to N whose total fare is at most S and whose total travel time is smallest; rides and villages may repeat.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Ivan has to pay out of his own pocket for the trip to the town where the next programming contest is held, and he has only euro. So he looked up the public transport timetables and the fares in advance.
Call Ivan's home village , the village where the contest takes place , and the other villages he may pass through . Ivan found bus lines. Each line connects village and village , takes hours in either direction, and costs euro per ride. Several buses may connect the same two villages, and those buses may differ in travel time and in fare.
Write a program that finds a route from village to village whose fares add up to at most euro. If several such routes exist, the program must find one where the total time spent sitting in buses is smallest.
Every ride on a line costs that line's fare and takes that line's time . A route may pass through the same village or the same line more than once, and each pass adds the fare and the time again.
If is , the start is already the destination, so the total time is .
Input
The first line contains the positive integers , and , with , and .
Each of the next lines contains four integers , , and describing one bus line, with , , and .
Output
Print the total travel time of the route found, on a single line. If no route costs at most euro, print -1.