Short on Fuel
Time limit1sMemory limit1024 MB
Given fuel depots on a grid, find the minimum starting fuel so a car moving only right or down can reach the goal, refueling at depots it passes.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Array
- Solved
- No attempts yet
Problem
Hyangbin, who had always dreamed of seeing the pyramids in person, signed up for a desert tour package. On the second day of the trip he arrived at the entrance of the desert and boarded the car used for the desert tour.
While he sat in the car waiting to depart, he found a desert tour guidebook. On the map inside the guidebook, the locations of fuel depots and the amount of fuel stored at each depot were marked.
Hyangbin was a car enthusiast who had memorized the fuel efficiency of every car, and he knew that the car he was riding consumes unit of fuel for every unit of distance it travels. The car only drives in directions parallel to the -axis or the -axis of the map. For example, if the car moves from to along a shortest path, it consumes units of fuel.
The car currently has no fuel, so he plans to refuel at the gas station here before departing. Because the fuel sold at the gas station is very expensive, Hyangbin will put in as little fuel as possible here and refuel afterward at the fuel depots he visits along the way.
Hyangbin wants to see the pyramids as soon as possible, so he asked the driver not to drive in the direction away from the pyramids. In other words, the car moves only right or down.
There is no limit on the amount of fuel that can be put in at the gas station, and there is no fuel depot at the current location or at the location of the pyramids. Also, no location has two or more fuel depots.
Because each fuel depot has a different location and amount of stored fuel, the amount of fuel that must be put in at the start can differ depending on the order in which the fuel depots are visited. Find the minimum amount of fuel that must be put in at the gas station for Hyangbin to travel from the current location to the point where the pyramids are.
Input
The first line gives the integers and , the height and width of the map. ()
The second line gives the integer , the number of fuel depots marked on the map. ()
Each of the next lines gives the integer coordinates of a fuel depot and the integer , the amount of fuel stored there. (, , )
Output
Print the minimum amount of fuel that must be put in at the gas station for Hyangbin to travel from the current location to the point where the pyramids are.