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 S euro. So he looked up the public transport timetables and the fares in advance.
Call Ivan's home village 1, the village where the contest takes place N, and the other villages he may pass through 2,3,…,N−1. Ivan found M bus lines. Each line connects village v and village w, takes t hours in either direction, and costs e 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 1 to village N whose fares add up to at most S 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 e and takes that line's time t. 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 N is 1, the start is already the destination, so the total time is 0.