Grid Speed

Time limit5sMemory limit128 MB

Summary
Given a grid of streets with speed limits, find the earliest arrival time within a time window and the most fuel-efficient trip, choosing speeds per segment.
Level

Medium7 of 10

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

Problem

Consider a grid-shaped city in which the north–south streets run on an elevated level above the east–west streets. Adjacent parallel streets are a fixed number of miles apart. Every street is two-way, and on- and off-ramps connect the two levels at every intersection, so switching between a north–south street and an east–west street takes no time. There are no traffic lights and almost no traffic.

Each street has its own speed limit, which is the same along the whole street and in both directions. Label an intersection by its column and row numbers, so the south-west corner is (1,1)(1, 1) and, in an n×nn \times n grid, the south-east corner is (n,1)(n, 1).

Fuel economy depends on speed. A car's speed is always a positive integer multiple of 55 miles per hour (mph). A car travelling at vv mph gets 80−0.03 v280 - 0.03\,v^2 miles per gallon (mpg).

For one trip from intersection (xs,ys)(x_s, y_s) to intersection (xt,yt)(x_t, y_t) you must choose the speed on every segment so that:

  • the car keeps a constant speed between two consecutive intersections (it may change speed only at an intersection);
  • the car never exceeds the speed limit of the street it is currently on;
  • the car travels the shortest possible distance between start and finish (that is, it never moves away from the destination); and
  • the car arrives within the allowed time interval.

For each trip, determine both the fastest way to arrive and the most fuel-efficient way to make the trip.

Input

The first line contains an integer tt, the number of scenarios.

Each scenario is given on five lines:

  1. an integer nn (n≤10n \le 10), the number of east–west streets and also the number of north–south streets;
  2. an integer gg (g<100g < 100), the spacing between adjacent parallel streets, in miles;
  3. nn integers: the speed limits of the east–west (horizontal) streets, from row 11 to row nn;
  4. nn integers: the speed limits of the north–south (vertical) streets, from column 11 to column nn;
  5. six integers xs ys xt yt a bx_s\ y_s\ x_t\ y_t\ a\ b: the start column and row, the target column and row, and the smallest and largest allowed travelling times a≤ba \le b in minutes (inclusive).

The largest speed limit is 5050. Both aa and bb are at most 10001000.

Output

For each scenario, first print the line

Scenario k:

where kk is the scenario number starting from 11.

If the trip cannot be completed within the allowed time interval, print a single line

IMPOSSIBLE

Otherwise print two more lines. On the second line, report the earliest arrival time that lies within the allowed interval, together with the least fuel needed to arrive at that time:

The earliest  arrival: T minutes, fuel F gallons

On the third line, report the smallest amount of fuel that can be used while still arriving within the interval, together with the earliest arrival time that uses that little fuel:

The economical travel: T minutes, fuel F gallons

Every arrival time TT is an integer number of minutes, rounded up. Every fuel amount FF is printed with exactly two digits after the decimal point. The spacing and punctuation must match the format above exactly (note the two spaces after earliest).

Examples2

  1. Example 1

    Input
    3
    8
    20
    10 20 30 40 50 50 50 50
    50 50 50 50 50 50 40 50
    2 3 7 8 300 320
    8
    2
    10 20 20 30 10 20 10 10 
    10 20 20 30 10 20 10 20 
    6 8 2 4 10 39
    10
    10
    30 20 20 10 10 20 10 10 20 20
    40 20 10 20 10 20 20 10 10 20
    1 1 10 10 100 500
    
    Expected output
    Scenario 1:
    The earliest  arrival: 300 minutes, fuel 6.25 gallons
    The economical travel: 318 minutes, fuel 5.60 gallons
    Scenario 2:
    IMPOSSIBLE
    Scenario 3:
    The earliest  arrival: 405 minutes, fuel 4.14 gallons
    The economical travel: 498 minutes, fuel 2.76 gallons
    
  2. Example 2

    Input
    1
    2
    10
    50 50
    50 50
    1 1 2 2 1 1000
    
    Expected output
    Scenario 1:
    The earliest  arrival: 24 minutes, fuel 4.00 gallons
    The economical travel: 240 minutes, fuel 0.25 gallons