Simple Polygon
Time limit2sMemory limit512 MB
Given n segments with one endpoint on the x-axis, decide whether they can all lie on the boundary of one simple polygon, and find the minimum boundary length if so.
- Level
Hard9 of 10
- Topics
- Geometry, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Let be a set of line segments. One endpoint of each segment lies on the x-axis, and the other lies above the x-axis. All endpoints are distinct, but segments may intersect at their interiors.
We want to draw a simple polygon such that every segment of is part of the boundary of . That is, each segment is either an edge of or part of an edge. A polygon is simple if the angle at each vertex is not , and no two edges meet except when two adjacent edges meet at a shared vertex.
Figure H.1 shows segment sets for which simple polygons can be drawn. The black solid segments belong to . Adding the red dotted segments gives a simple polygon. The left figure has every endpoint of as a vertex of . The right figure has a simple polygon with 6 vertices, where two endpoints each lie in the interior of an edge of . An endpoint of does not have to be a vertex of .
Figure H.1: Examples of line segments for which simple polygons can be drawn.
Figure H.2 shows segment sets for which no simple polygon can be drawn.
Figure H.2: Examples of line segments for which no simple polygon can be drawn.
If more than one simple polygon exists for a set, we want the one with the minimum boundary length. Figure H.3 shows a simple polygon whose boundary is shorter than that of any other simple polygon for its set.
Figure H.3: A simple polygon with the minimum boundary length.
Given a set of line segments, determine whether a simple polygon exists for the set. If it does, find a simple polygon with the minimum boundary length.
Input
The first line contains an integer (), the number of segments. Each of the next lines contains three integers , , and (). These describe the segment with endpoints and . All endpoints are distinct, so no two endpoints occupy the same position.
Output
Print exactly one line. If a simple polygon exists, print the minimum boundary length rounded to one decimal place. Otherwise, print .