City Road Map
Time limit20sMemory limit128 MB
Build a graph from road segments with one-way restrictions imposed by perpendicular or angled sign segments, then find the unique shortest path between two points.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Geometry, Implementation
- Solved
- No attempts yet
Problem
Given the road map of a city, write a program that finds the shortest route between two given points.
The road map is represented as a set of line segments in the plane. Some segments are roads; the others are signs that mark a direction in which you may not travel. A segment is a sign if exactly one of its two endpoints lies on another segment while the other endpoint does not; every other segment is a road.
A sign attached at a point on a road restricts travel through . Suppose the road passes through points and on opposite sides of , and let be the sign's free endpoint. Among the angles the sign makes with the road, you may not move from the side of the obtuse angle toward the side of the acute angle. For example, if angle is acute and angle is obtuse, you may travel from through to , but not from through to . If the sign meets the road at a right angle, you may not pass through at all.
Find the shortest route from the start point to the goal point that obeys these rules. The distance between two points and is .
Input
The first line contains the number of test cases . Each test case has the following form.
The first line of a test case contains the number of segments (). The second line contains and , and the third line contains and , where is the start point and is the goal point; you must compute the shortest route between them. The start and goal points are different, and the shortest route is always unique.
Each of the next lines describes one segment in the form , the two endpoints of the -th segment. The two endpoints of a segment are different, and no two segments cross; that is, segments always meet at a shared endpoint. All coordinates are non-negative integers not greater than 1000.
Output
For each test case, print the points of the shortest route from the start point to the goal point, in the order they are visited: the start point first, then every point on the route where at least two roads meet, and the goal point last. Print one point per line as its x-coordinate and y-coordinate separated by a single space, then print 0 on the final line of that test case. An intersection point is a point where at least two roads (segments that are not signs) meet, so a point where only a sign touches a road is not printed. If there is no route from the start point to the goal point, print -1 as the only output for that test case.