City Road Map

Time limit20sMemory limit128 MB

Summary
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 BB on a road restricts travel through BB. Suppose the road passes through points AA and CC on opposite sides of BB, and let FF 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 ABFABF is acute and angle CBFCBF is obtuse, you may travel from AA through BB to CC, but not from CC through BB to AA. If the sign meets the road at a right angle, you may not pass through BB at all.

Find the shortest route from the start point to the goal point that obeys these rules. The distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is (x2−x1)2+(y2−y1)2\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}.

Input

The first line contains the number of test cases TT. Each test case has the following form.

The first line of a test case contains the number of segments nn (1≤n≤2001 \le n \le 200). The second line contains xsx_s and ysy_s, and the third line contains xgx_g and ygy_g, where (xs,ys)(x_s, y_s) is the start point and (xg,yg)(x_g, y_g) 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 nn lines describes one segment in the form x1k y1k x2k y2kx_{1k}\ y_{1k}\ x_{2k}\ y_{2k}, the two endpoints of the kk-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.

Examples1

  1. Example 1

    Input
    3
    8
    1 1
    4 4
    1 1 4 1
    1 1 1 4
    3 1 3 4
    4 3 5 3
    2 4 3 5
    4 1 4 4
    3 3 2 2
    1 4 4 4
    9
    1 5
    5 1
    5 4 5 1
    1 5 1 1
    1 5 5 1
    2 3 2 4
    5 4 1 5
    3 2 2 1
    4 2 4 1
    1 1 5 1
    5 3 4 3
    11
    5 5
    1 0
    3 1 5 1
    4 3 4 2
    3 1 5 5
    2 3 2 2
    1 0 1 2
    1 2 3 4
    3 4 5 5
    1 0 5 2
    4 0 4 1
    5 5 5 1
    2 3 2 4
    
    Expected output
    1 1
    3 1
    3 4
    4 4
    0
    -1
    5 5
    5 2
    3 1
    1 0
    0