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.
Hard8GeometryBrute forceImplementationMathNo attempts yetTime limit5sMemory limit512 MBThe 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 s in the west part to a point t in the east part. A highway is one bridge over the river plus two roads. One road joins s to the west end of the bridge, and the other joins t 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.
Write a program that computes the total length of a highway meeting both conditions.
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 (x,y), where x is the distance from the west side and y is the distance from the north side.
The first line contains four integers sx, sy, tx, ty (0≤sx,sy,tx,ty≤1000). Point s is at (sx,sy) and point t is at (tx,ty). The next line contains an integer N (2≤N≤20), the number of points that make up the west riverside. Each of the next N lines contains two integers wxi and wyi (0≤wxi,wyi≤1000), and the i-th point of the west riverside is (wxi,wyi). The west riverside is the polygonal line built from the segments between (wxi,wyi) and (wxi+1,wyi+1) for all 1≤i≤N−1. The next line contains an integer M (2≤M≤20), the number of points that make up the east riverside. Each of the next M lines contains two integers exi and eyi (0≤exi,eyi≤1000), and the i-th point of the east riverside is (exi,eyi). The east riverside is built the same way.
The input satisfies the following conditions.
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.