Under construction

Still being built. Things may change

Airport Logistics

Time limit2sMemory limit512 MB

Summary
Find the minimum travel time from A to B when moving at speed 1 m/s on the floor and 2 m/s along straight conveyor segments that can be entered or left at any point.
Level

Medium7 of 10

Topics
Shortest path, Geometry, Graph, Math
Solved
No attempts yet

Problem

Many airports have moving walkways in the corridors between halls and terminals. Instead of walking on the floor, a passenger can stand on a walkway, and walking along one gets you to the end of the corridor much faster.

Delft City Airport uses the same idea. Following the latest fashion in airport architecture, it has no corridors at all. The whole airport is one big hall, and the conveyors lie on the floor at arbitrary positions and in arbitrary directions.

To get from a point AA to a point BB, a passenger may mix walking on the floor and walking on conveyors any way they like. You can hop on or off a conveyor at any point along it, and you can cross a conveyor without standing on it.

Walking on the floor goes at 1 meter per second. Walking forward on a conveyor goes at a total speed of 2 meters per second.

Walking against the direction of a conveyor gains nothing and is forbidden, but you may walk on the floor immediately next to a conveyor. Conveyors are infinitely thin.

Find the minimum time needed to get from AA to BB.

Figure 1: the fastest route for the first sample input.

Input

The first line contains four floating point numbers XAX_A, YAY_A, XBX_B, YBY_B. The start is A=(XA,YA)A = (X_A, Y_A) and the destination is B=(XB,YB)B = (X_B, Y_B).

The second line contains an integer NN, the number of conveyors in the hall (0≤N≤1000 \le N \le 100). Each of the following NN lines contains four floating point numbers X1X_1, Y1Y_1, X2X_2, Y2Y_2. That conveyor starts at (X1,Y1)(X_1, Y_1), ends at (X2,Y2)(X_2, Y_2), and runs in a straight line from its start to its end.

All coordinates are floating point numbers in meters with 0≤X,Y≤1000.00 \le X, Y \le 1000.0.

Every conveyor is at least 1 meter long. No two conveyors intersect or touch. Neither the start nor the destination lies on a conveyor.

Output

Print on one line the minimum time in seconds needed to get from AA to BB, rounded to six digits after the decimal point.

The judge compares your output as text, so compute the time accurately enough that the sixth digit is right.

Examples4

  1. Example 1

    Input
    60.0 0.0 50.0 170.0
    3
    40.0 0.0 0.0 0.0
    5.0 20.0 5.0 170.0
    95.0 0.0 95.0 80.0
    
    Expected output
    168.791651
    
  2. Example 2

    Input
    60.0 0.0 50.0 170.0
    3
    40.0 0.0 0.0 0.0
    5.0 20.0 5.0 170.0
    95.0 0.0 95.0 100.0
    
    Expected output
    163.527474
    
  3. Example 3

    Input
    0.0 1.0 4.0 1.0
    1
    0.0 0.0 4.0 0.0
    
    Expected output
    3.732051
    
  4. Example 4

    Input
    0.0 1.0 10.0 1.0
    2
    1.0 0.0 2.0 3.0
    6.0 1.0 4.0 1.0
    
    Expected output
    10.000000