Motorways
Time limit1sMemory limit128 MB
Each directed toll changes linearly by day; find the day among the first d that minimizes the cost of a round trip from a to b and back.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Binary search, Math
- Solved
- No attempts yet
Problem
Byteland is covered by a dense network of paid two-way motorways. Each motorway connects two cities. The toll changes from day to day and depends both on which motorway is taken and on the direction of travel. Over the days each toll changes in a linear way: for every motorway and every direction there is a constant such that the toll for that direction changes by each day. The constant may be negative (the toll decreases), zero (the toll stays the same), or positive (the toll increases). All tolls change at midnight.
ByteGuy lives in city and wants to visit his friend, who lives in city . On a single day he must drive from to and then back from to . Being thrifty, he wants to make the trip on the day when the total cost of using the motorways is as small as possible. He must visit his friend no later than the -th day.
The motorway network of Byteland is so well developed that it is possible to travel between any pair of cities, and between each pair of cities there is at most one direct connection. Byteland is a small country, so using the cheapest route he can always drive to city and return home within a single day. During the first days every toll is guaranteed to be positive.
Write a program that:
- reads the descriptions of the motorways together with ByteGuy's home city , his friend's city , and the number of days ;
- finds the minimum cost of driving from to and back on one of the first days;
- writes the result to standard output.
Input
The first line contains five integers , , , , (, , ), where is the number of cities, is the number of motorways, is ByteGuy's home city, is his friend's city, and is the number of days within which the trip must be made. The cities are numbered from to , and and are different.
Each of the next lines describes one motorway with six integers , , , , , . Cities and are the endpoints of the motorway. and are the tolls on the first day for driving from to and from to , respectively. On each following day the first toll changes by and the second toll changes by . During the considered period (days through ) every toll is positive and at most .
Output
Print a single integer: the minimum cost of driving from to and back on one of the first days.
Hint

In the figure above, to distinguish the costs of the two travel directions, each motorway is drawn as a pair of directed edges. The pair of numbers next to each edge gives the toll on the first day and the daily change of that toll.
Taking the route on the second day costs .