#P3218. Fuel‐Optimal Racing
Fuel‐Optimal Racing
Fuel‐Optimal Racing
In this problem, you are given a racing game scenario. A car has an initial fuel amount f, and the track from start to finish is composed of n line segments. Each segment is characterized by its length L (in kilometers) and its slope s. The car is allowed to change its speed instantly at any point along the track. When driving on a road with slope s at speed v (in km/h), the fuel consumption per kilometer is given by
where a and b (with b > 0) are given constants. Note that if the expression a·v+b·s is non‐positive, then no fuel is consumed on that kilometer.
The vehicle cannot exceed a maximum speed of vmax km/h. Your task is to decide how to vary the speed along the track so as to minimize the total travel time, while never using more than the initial fuel amount f (there is no refuelling during the race).
Important details:
- You may choose a different speed for each point on the road, even within the same segment.
- On a segment with negative slope (i.e. downhill), it is possible to avoid fuel consumption entirely by keeping the speed not greater than \(-\frac{b\,s}{a}\). However, going too slowly increases the travel time.
- For segments where the fuel consumption function is always positive (for example, flat or uphill), you must use fuel and the consumption per kilometer is a·v+b·s.
The input guarantees that it is always feasible to finish the race (i.e. even by driving very slowly on the fuel‐consuming segments, the total fuel consumed cannot be lower than a certain positive amount).
Your output should be the minimum total travel time needed to complete the race. Answers within an absolute or relative error of 1e-6 are accepted.
inputFormat
The input is read from standard input and has the following format:
f v_max a b n L₁ s₁ L₂ s₂ … Lₙ sₙ
Here,
- f is the initial fuel available (a floating point number).
- v_max is the maximum allowed speed in km/h (a floating point number).
- a and b are parameters in the fuel consumption function (floating point numbers, with b > 0).
- n is the number of segments on the track (an integer).
- Each of the next n lines contains two numbers: L (the length of the segment in kilometers) and s (the slope of the segment).
outputFormat
Output a single number: the minimum total travel time to complete the race. The answer is accepted if its absolute or relative error is no more than 1e-6.
sample
100 100 1 1 3
10 0
20 1
30 -1
1.103412
</p>