Electric Vehicle
Time limit2sMemory limit512 MB
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 villages, referred to as points on a plane. For any two villages, there is exactly one road between them, so there are roads in total. Let two villages and have coordinates and . The length of the road between them is , which is called the distance between and . For convenience, the distance between and 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 be the cost per unit of electricity when an EV is charged in village . An EV with zero charge at village must be charged at a cost of at least in to reach village .
Minsu must travel from village to village in his EV. The EV is discharged, with zero charge, at before departure. The battery has a maximum charge capacity , so the EV cannot store more than units of electricity. A fully charged EV can move a distance of . On the way from to , Minsu may make at most stops for recharging. Charging at the starting village counts as one of these stops. Find the cheapest way for Minsu to travel from to .
Input
The first line contains an integer (), the number of villages. Each of the next lines contains three integers , , and ( and ), where is the coordinate of a village and is the charging cost at that village. The first of these lines gives the starting village , and the second gives the destination . All village coordinates are distinct. The next line contains an integer (), the maximum charge capacity of the battery. The last line contains a positive integer (), the maximum number of times the EV may recharge during the trip from to . Charging at counts as one of these stops.
Output
Print exactly one line containing the cost of the cheapest way for the EV to travel from to with at most stops for recharging. If no such way exists, print -1.