This page is still under construction.

Parts of this page are still being built. What you see may change.

Motorways

Time limit1sMemory limit128 MB

Summary
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 pp such that the toll for that direction changes by pp each day. The constant pp 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 aa and wants to visit his friend, who lives in city bb. On a single day he must drive from aa to bb and then back from bb to aa. 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 dd-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 bb and return home within a single day. During the first dd days every toll is guaranteed to be positive.

Write a program that:

  • reads the descriptions of the motorways together with ByteGuy's home city aa, his friend's city bb, and the number of days dd;
  • finds the minimum cost of driving from aa to bb and back on one of the first dd days;
  • writes the result to standard output.

Input

The first line contains five integers nn, mm, aa, bb, dd (2≤n≤100 0002 \le n \le 100\,000, 1≤m≤100 0001 \le m \le 100\,000, 2≤d≤10 0002 \le d \le 10\,000), where nn is the number of cities, mm is the number of motorways, aa is ByteGuy's home city, bb is his friend's city, and dd is the number of days within which the trip must be made. The cities are numbered from 11 to nn, and aa and bb are different.

Each of the next mm lines describes one motorway with six integers n1n_1, n2n_2, c1c_1, p1p_1, c2c_2, p2p_2. Cities n1n_1 and n2n_2 are the endpoints of the motorway. c1c_1 and c2c_2 are the tolls on the first day for driving from n1n_1 to n2n_2 and from n2n_2 to n1n_1, respectively. On each following day the first toll changes by p1p_1 and the second toll changes by p2p_2. During the considered period (days 11 through dd) every toll is positive and at most 10 00010\,000.

Output

Print a single integer: the minimum cost of driving from aa to bb and back on one of the first dd 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 1→2→3→4→11 \to 2 \to 3 \to 4 \to 1 on the second day costs 2323.

Examples2

  1. Example 1

    Input
    4 4 1 4 3
    1 2 5 -1 10 -1
    3 2 12 2 7 2
    3 4 8 -1 20 -3
    1 4 27 -2 3 0
    
    Expected output
    23
    
  2. Example 2

    Input
    2 1 1 2 2
    1 2 10 0 20 0
    
    Expected output
    30