Roll a Big Ball

Time limit1sMemory limit128 MB

Summary
Find the largest radius of a ball that rolls along a straight course without hitting any axis-aligned rectangular block.
Level

Hard8 of 10

Topics
Geometry, Binary search, Math, Implementation
Solved
No attempts yet

Problem

Every July, ACM University holds its sports day, and its highlight is "Roll-A-Big-Ball". In this game, players roll a ball along a straight course drawn on the ground. Rectangular parallelepiped blocks are fixed on the ground as obstacles. During the game the ball must not collide with any block, and the ball's bottom point must not leave the course.

To make the game more fun, the university wants to use the largest possible ball. Write a program that finds the largest radius of a ball that can reach the goal without colliding with any obstacle block.

The ball is a perfect sphere and the ground is a plane. Each block is a rectangular parallelepiped whose bottom rectangle lies on the ground with edges parallel to the x- or y-axis. The course is given as a line segment from a start point to an end point. The ball starts with its bottom point touching the start point and reaches the goal when its bottom point touches the end point.

Input

The input consists of several datasets. Each dataset has the following format.

N
sx sy ex ey
minx1 miny1 maxx1 maxy1 h1
minx2 miny2 maxx2 maxy2 h2
...
minxN minyN maxxN maxyN hN

The first line of a dataset holds an integer NN (1≤N≤501 \le N \le 50), the number of blocks. The next line holds four space-separated integers giving the start point (sx,sy)(sx, sy) and the end point (ex,ey)(ex, ey). Each of the following NN lines describes one block with five space-separated integers: the two vertices (minx,miny)(minx, miny), (maxx,maxy)(maxx, maxy) of its bottom rectangle and the block height hh. All integers satisfy the following conditions.

  • −10000≤sx,sy,ex,ey≤10000-10000 \le sx, sy, ex, ey \le 10000
  • −10000≤minxi<maxxi≤10000-10000 \le minx_i < maxx_i \le 10000
  • −10000≤minyi<maxyi≤10000-10000 \le miny_i < maxy_i \le 10000
  • 1≤hi≤10001 \le h_i \le 1000

The last dataset is followed by a line containing a single zero.

Output

For each dataset, output on its own line the largest radius, rounded to exactly 6 digits after the decimal point. If any block lies on the course line, the largest radius is defined to be zero. You may assume that the largest radius never exceeds 1000 for each dataset.

Examples3

  1. Example 1

    Input
    2
    -40 -40 100 30
    -100 -100 -50 -30 1
    30 -70 90 -30 10
    2
    -4 -4 10 3
    -10 -10 -5 -3 1
    3 -7 9 -3 1
    2
    -40 -40 100 30
    -100 -100 -50 -30 3
    30 -70 90 -30 10
    2
    -400 -400 1000 300
    -800 -800 -500 -300 7
    300 -700 900 -300 20
    3
    20 70 150 70
    0 0 50 50 4
    40 100 60 120 8
    130 80 200 200 1
    3
    20 70 150 70
    0 0 50 50 4
    40 100 60 120 10
    130 80 200 200 1
    3
    20 70 150 70
    0 0 50 50 10
    40 100 60 120 10
    130 80 200 200 3
    1
    2 4 8 8
    0 0 10 10 1
    1
    1 4 9 9
    2 2 7 7 1
    0
    
    Expected output
    30.000000
    1.000000
    18.166667
    717.785714
    50.500000
    50.000000
    18.166667
    0.000000
    0.000000
    
  2. Example 2

    Input
    1
    0 0 100 0
    40 10 60 30 5
    0
    
    Expected output
    12.500000
    
  3. Example 3

    Input
    1
    0 0 100 0
    40 -10 60 10 5
    0
    
    Expected output
    0.000000