Under construction

Still being built. Things may change

Scientists' Regatta

Time limit2sMemory limit512 MB

Summary
Given start, finish, and non-intersecting segment obstacles in the plane, compute the shortest path that never crosses a segment interior.
Level

Hard8 of 10

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

Problem

Every year since 1996, computer scientists from all over the world gather for the famous Scientists' Regatta. The competition is a boat race with obstacles on the ocean. All teams start from a common point, and each team must reach the finish point without touching or crossing any obstacle. A team that touches or crosses an obstacle is disqualified immediately. The winning team is the first one to reach the finish point. The finish point is different from the starting point.

The Brazilian team has hired you to write a program that computes the length of the shortest valid route from the starting point to the finish point.

The ocean is an infinite plane. Each obstacle is at a fixed position and is a line segment given by its two endpoints (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2). Boats have no size (each boat is a point in the plane), and obstacles have negligible thickness.

No two obstacles intersect. Likewise, neither the starting point nor the finish point lies on any obstacle.

A boat can pass as close to an obstacle as it wants. So the value to compute is the infimum of the lengths of valid routes. It equals the shortest distance computed as if a route may pass through obstacle endpoints and run along obstacles. A route still may not cross the interior of an obstacle.

Input

The input contains several test cases. The first line of each test case contains five integers xix_i, yiy_i, xfx_f, yfy_f, and nn: the coordinates of the starting point (xi,yi)(x_i, y_i), the coordinates of the finish point (xf,yf)(x_f, y_f), and the number of obstacles nn (0≤n≤150)(0 \le n \le 150). Each of the next nn lines contains four integers x1x_1, y1y_1, x2x_2, and y2y_2, the coordinates of the two endpoints of one obstacle. The coordinates xx and yy of every point satisfy −5000≤x,y≤5000-5000 \le x, y \le 5000.

A line with xi=yi=xf=yf=n=0x_i = y_i = x_f = y_f = n = 0 marks the end of the input.

Output

For each test case, print one line with the length of the shortest valid route, rounded to two decimal places. Always print exactly two digits after the decimal point.

Examples1

  1. Example 1

    Input
    0 0 10 0 1
    5 -1 5 1
    0 0 10 0 1
    5 0 5 1
    0 0 0 0 0
    
    Expected output
    10.20
    10.00