Hill Drive

Time limit1sMemory limit128 MB

Summary
Given fuel and a per-road speed-dependent consumption model with a speed cap, choose speeds per road segment to minimize total travel time under a total fuel budget.
Level

Hard8 of 10

Topics
Binary search, Greedy, Math
Solved
No attempts yet

Problem

Sanggeun has driven up a nearby mountain and now wants to get back home as quickly as possible. His car is low on fuel, so he has to drive as efficiently as he can.

Part of the way home is uphill and part is downhill. Every road segment has its own length and slope. Given how much fuel is left in the car, determine the shortest possible time to get home.

The car's fuel usage is modeled simply. The fuel consumption cc (liters per km) grows in proportion to the speed vv and is offset by the slope ss of the road:

c=max⁡(0, αv+βs)c = \max(0,\ \alpha v + \beta s)

Here α\alpha is the flat-ground consumption factor, vv is the speed in km/h, ss is the slope of the road, and β\beta is a positive constant. For intuition, suppose a hill can be descended at 1010 km/h while burning no fuel; driving up that same hill then uses as much fuel as driving 1010 km/h faster on flat ground. Accelerating and decelerating use no fuel and happen instantly. The car also has a top speed vmaxv_{max} that it can never exceed.

Distances are given in meters. For a road segment with horizontal length xx and height change yy, the actual distance driven is x2+y2\sqrt{x^2 + y^2} meters and its slope is s=y/xs = y / x. Speeds are in km/h and the travel time is measured in hours.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100). Each test case is given as follows.

The first line of a test case contains four real numbers α\alpha (0.5≤α≤1000.5 \le \alpha \le 100), β\beta (0.1≤β≤1000.1 \le \beta \le 100), vmaxv_{max} (10≤vmax≤20010 \le v_{max} \le 200), and ff (0≤f≤500 \le f \le 50), where vmaxv_{max} is the car's top speed in km/h and ff is the fuel left in liters.

The next line contains the number of roads rr (1≤r≤100001 \le r \le 10000).

Each of the next rr lines contains two real numbers xix_i and yiy_i (1≤xi≤10001 \le x_i \le 1000, −1000≤yi≤1000-1000 \le y_i \le 1000): the horizontal length and the height change of the ii-th road, both in meters. Each road has a constant slope.

Output

For each test case, print on its own line the minimum time in hours needed to get home, rounded to exactly 6 digits after the decimal point (for example, printf("%.6f")). If it is impossible to get home with the available fuel, print IMPOSSIBLE instead. When getting home is possible, the required time is always less than 24 hours. The test data is chosen so that the exact answer is never close to a rounding boundary.

Examples4

  1. Example 1

    Input
    3
    10.0 1.0 150 0.0
    1
    100.0 -100.0
    10.0 100.0 150 1.0
    2
    100 0
    100 100
    0.5 0.1 100 10
    3
    1000 0
    100 10
    100 -10
    
    Expected output
    1.414214
    IMPOSSIBLE
    0.072120
    
  2. Example 2

    Input
    1
    2.0 1.0 120 30
    1
    800 0
    
    Expected output
    0.042667
    
  3. Example 3

    Input
    1
    5.0 20.0 100 2.0
    1
    300 250
    
    Expected output
    IMPOSSIBLE
    
  4. Example 4

    Input
    2
    1.5 2.0 100 18
    2
    900 10
    200 -30
    6.0 0.5 160 40
    2
    1000 0
    150 120
    
    Expected output
    0.101026
    0.213573