Fences Make Good Neighbors
Time limit4sMemory limit2048 MB
Triangulate a convex polygon so that exactly two fences separate the two brothers' parcels, avoiding fences through their houses, and minimize total fence length.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry
- Solved
- No attempts yet
Problem
The King of Gletrian has a large estate that he wants to divide into triangular parcels, which he will give to deserving (that is, wealthy) followers. The estate is a convex polygon with fences already along its borders, so the only cost is the new fencing used to partition the land. Every new fence runs in a straight line between existing corners of the estate, and no two fences cross. Being fiscally minded (that is, cheap), the King wants to use as little fencing as possible.
There is a problem. He has two sons who already live on the estate. One is a cute young lad, and the other is somewhat obtuse. They do not get along. A single fence between their two parcels is out of the question, because it would lead to constant bickering and possibly a physical altercation. The King hopes his sons will learn to appreciate one another, and he believes they need one good arbiter to serve as a liaison. So he wants exactly two fences between the brothers' parcels, with a single parcel between them where he will place a conscripted volunteer to serve as go-between. He still wants to minimize the total fencing. No fence that passes directly through a brother's house may be used in the triangulation.
Figure E.1 shows Sample Input 1, with the brothers' locations marked by plus signs. The partition on the right uses less fencing, but it is not a solution because more than two fences lie between the brothers' locations. The correct triangulation is on the left.

Figure E.1: Sample Input 1. (a) Correct solution. (b) Incorrect solution.
Input
The input starts with a single integer (), the number of corners of the estate. Next come pairs of integers () giving the location of each corner, in clockwise order. No two corners coincide, the polygon is convex, and no three consecutive corners are collinear. The last two lines each contain a pair of coordinates: for the first brother and for the second (). The two brothers' locations are distinct and lie in the interior of the polygon. All coordinates are in kilometers.
Output
Output the minimum total fence length in kilometers that satisfies all the conditions above. Answers within an absolute error of of the judges' answer are accepted. If the conditions cannot be satisfied, output the word IMPOSSIBLE.