The Great Traveler
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

As a great traveler, your favorite thing to do is hit the road and explore. Today, you have arrived in a new country consisting of n cities and m bidirectional roads. These roads are structured such that each city is connected to at most r bidirectional roads. After spending some time in City 1, you sense a strange, eerie atmosphere and decide you need to go to City n to leave the country.

Suddenly, you realize that these bidirectional roads are subject to a mysterious restriction: each road has a fluctuation value p_i and an energy cost q_i. Whenever you exit a road, your personal fluctuation value pp is updated to match that road's fluctuation value p_i. While traveling, if your current fluctuation value is pp, you can only enter a road whose fluctuation value falls between pp-k and pp+k inclusive. The cost of traversing a road is equal to its energy value.

Given that you can choose any initial fluctuation value to start your journey, you want to find out if it is possible to travel from City 1 to City n. If it is possible, output the minimum total cost required; otherwise, output -1 to indicate that City n is unreachable.

输入描述:

The first line contains four integers: n,m,k,r
(1\le n \le 10^5,0\le m \le 2\times 10^5,0\le k \le 10^9, 1\le r\le 50), as described in the problem statement.

The following m lines each contain four integers: u_i,v_i,p_i,q_i 
(1\le u_i,v_i\le n,u_i\neq v_i,0\le p_i\le 10^9,1\le q_i\le 10^9), representing the two cities connected by the bidirectional road, its fluctuation value, and its energy cost, respectively. It is guaranteed that there is at most one road between the same pair of cities.

输出描述:

If City n is reachable from City 1, output an integer representing the minimum total cost. Otherwise, output -1.
示例1

输入

复制
5 9 5 10
5 3 7 9
5 2 10 6
2 4 3 1
4 5 10 1
2 3 8 1
2 1 7 4
1 3 4 1
3 4 6 3
4 1 7 6

输出

复制
5

说明

For the sample case, a feasible optimal path is as follows: set the initial fluctuation value to 4, pass through edge (1,3), then pass through edge (3,4) (at which point the fluctuation value becomes 6), and finally pass through edge (4,5). The total energy value consumed is 1+3+1=5.