Shortest Bridge
Time limit5sMemory limit512 MB
Given two polygonal riverbanks and points s and t on opposite sides, minimize the bridge length between the banks, then the road lengths from s and t to its endpoints.
- Level
Hard8 of 10
- Topics
- Geometry, Brute force, Implementation, Math
- Solved
- No attempts yet
Problem
The city is a square 1,000 units on a side. A large river runs through it from north to south and splits the city into exactly two parts, the west part and the east part.
The mayor has decided to build a highway from a point in the west part to a point in the east part. A highway is one bridge over the river plus two roads. One road joins to the west end of the bridge, and the other joins to the east end. The bridge is a straight segment between a point on the west riverside and a point on the east riverside. A road does not have to be a straight line, but the length of its intersection with the river must be zero.
To keep the cost down, the mayor builds a highway that meets both of these conditions.
- A bridge costs more than a road, so the length of the bridge joining the west part and the east part must be as short as possible.
- Under that condition, the sum of the lengths of the two roads must be as small as possible.
Write a program that computes the total length of a highway meeting both conditions.
Input
The input is a single test case in the following format.
sx sy tx ty
N
wx1 wy1
:
:
wxN wyN
M
ex1 ey1
:
:
exM eyM
A point in the city is written as a coordinate , where is the distance from the west side and is the distance from the north side.
The first line contains four integers , , , (). Point is at and point is at . The next line contains an integer (), the number of points that make up the west riverside. Each of the next lines contains two integers and (), and the -th point of the west riverside is . The west riverside is the polygonal line built from the segments between and for all . The next line contains an integer (), the number of points that make up the east riverside. Each of the next lines contains two integers and (), and the -th point of the east riverside is . The east riverside is built the same way.
The input satisfies the following conditions.
- and are 0, and and are 1,000.
- Neither polygonal line intersects itself.
- The west riverside and the east riverside do not meet.
- Point lies in the west part of the city. That is, lies in the region bounded by the sides of the square and the west polygonal line that does not contain the points of the east riverside.
- Point lies in the east part of the city. That is, lies in the region bounded by the sides of the square and the east polygonal line that does not contain the points of the west riverside.
- Each polygonal line meets the square only at its two end points. In other words, holds for , and holds for .
Output
Print the length of the bridge and the total length of the highway on one line, separated by a single space. The total length of the highway is the bridge plus the two roads. Print both values with exactly four digits after the decimal point.