Under construction

Still being built. Things may change

Electric Vehicle

Time limit2sMemory limit512 MB

Summary
Find the cheapest trip from S to T on a complete graph of villages with Manhattan distances, using at most Δ recharges and battery capacity W.
Level

Medium6 of 10

Topics
Dynamic programming, Shortest path, Graph
Solved
No attempts yet

Problem

An electric vehicle (EV) is an alternative fuel automobile that uses one or more electric motors for propulsion, in place of an internal combustion engine such as a diesel or gasoline engine. The EV stores electricity in an energy storage device, such as a battery, and the electricity powers the vehicle's wheels through electric motors. Its energy storage capacity is limited, so the EV must be recharged regularly by plugging into an electrical source.

There are nn villages, referred to as points on a plane. For any two villages, there is exactly one road between them, so there are n×(n−1)/2n \times (n-1)/2 roads in total. Let two villages AA and BB have coordinates (a,b)(a, b) and (c,d)(c, d). The length of the road between them is d(A,B)=∣a−c∣+∣b−d∣d(A, B) = |a-c| + |b-d|, which is called the distance between AA and BB. For convenience, the distance between AA and BB equals the amount of electricity an EV needs to move between them. That is, while an EV consumes electricity 1, it can move distance 1.

Each village has a charging station for an EV, and the charging cost varies from village to village. Let c(A)c(A) be the cost per unit of electricity when an EV is charged in village AA. An EV with zero charge at village AA must be charged at a cost of at least c(A)⋅d(A,B)c(A) \cdot d(A, B) in AA to reach village BB.

Minsu must travel from village SS to village TT in his EV. The EV is discharged, with zero charge, at SS before departure. The battery has a maximum charge capacity WW, so the EV cannot store more than WW units of electricity. A fully charged EV can move a distance of WW. On the way from SS to TT, Minsu may make at most Δ\Delta stops for recharging. Charging at the starting village SS counts as one of these stops. Find the cheapest way for Minsu to travel from SS to TT.

Input

The first line contains an integer nn (2≤n≤1,0002 \le n \le 1{,}000), the number of villages. Each of the next nn lines contains three integers aa, bb, and cc (0≤a,b≤1060 \le a, b \le 10^6 and 1≤c≤1041 \le c \le 10^4), where (a,b)(a, b) is the coordinate of a village and cc is the charging cost at that village. The first of these lines gives the starting village SS, and the second gives the destination TT. All village coordinates are distinct. The next line contains an integer WW (1≤W≤1051 \le W \le 10^5), the maximum charge capacity of the battery. The last line contains a positive integer Δ\Delta (1≤Δ≤101 \le \Delta \le 10), the maximum number of times the EV may recharge during the trip from SS to TT. Charging at SS counts as one of these stops.

Output

Print exactly one line containing the cost of the cheapest way for the EV to travel from SS to TT with at most Δ\Delta stops for recharging. If no such way exists, print -1.

Examples3

  1. Example 1

    Input
    4
    0 0 1
    3 0 3
    1 0 3
    2 0 3
    4
    2
    
    Expected output
    3
    
  2. Example 2

    Input
    5
    1 1 4
    3 3 3
    1 3 4
    2 2 5
    3 1 3
    3
    2
    
    Expected output
    14
    
  3. Example 3

    Input
    5
    1 1 4
    3 3 3
    1 3 4
    2 2 5
    3 1 3
    3
    1
    
    Expected output
    -1