This page is still under construction.

Parts of this page are still being built. What you see may change.

Islands

Time limit3sMemory limit128 MB

Summary
Given non-overlapping simple polygons as islands, find the minimum total water distance to travel from one island to another, since walking on land is free.
Level

Hard8 of 10

Topics
Geometry, Shortest path, Graph
Solved
No attempts yet

Problem

The financial crisis in Greece has major consequences for the Greeks, especially for those who live on one of the many islands. Some of them cannot even afford to travel from one island to another by boat. They avoid going to another island as much as possible, but if they really must, they have to swim.

Since swimming is very exhausting and potentially dangerous, they would like to minimize the distance they have to swim. In that regard, swimming directly from island A to B may not be the best option. Instead, it might be better to swim from A to C, cross island C on foot, and then swim from C to B. The ideal travel plan could in fact involve many islands.

You are given a collection of islands, modeled as simple polygons. For two given islands, determine the smallest possible total distance one needs to swim in order to get from one island to the other. The total distance covered on land does not matter.

Input

The first line contains one positive integer: the number of test cases, at most 100100. Then, for each test case:

  • one line with one integer nn (2≤n≤502 \le n \le 50): the number of islands.
  • one line with two space-separated integers ss and dd (1≤s,d≤n1 \le s, d \le n, s≠ds \ne d): the start and destination islands of the intended journey, respectively.
  • then, for each island:
    • one line with one integer mm (3≤m≤503 \le m \le 50): the number of vertices of the polygon describing the island.
    • mm lines, each with two space-separated integers xix_i and yiy_i (−10000≤xi,yi≤10000-10000 \le x_i, y_i \le 10000): the coordinates of the ii-th vertex.

The polygons (islands) are non-self-intersecting and do not overlap or touch each other. The vertices of each polygon are given in counterclockwise order.

Output

For each test case:

  • one line with one floating-point number: the minimum distance one needs to swim to get from the start island to the destination island, rounded to three decimal places.

The test data are such that an absolute error of at most 10−610^{-6} in the final answer does not change the result of the rounding.

Hint

The best route is not always a direct swim between the two islands. It can be shorter to swim to a nearby island, cross it on foot for free, and continue from there, so the optimal plan may pass through several intermediate islands. Only the segments crossed over water count toward the distance.

Examples2

  1. Example 1

    Input
    2
    3
    1 3
    4
    0 0
    1 0
    1 1
    0 1
    4
    2 2
    3 2
    3 3
    2 3
    4
    4 0
    5 0
    5 1
    4 1
    4
    1 2
    3
    0 0
    10 0
    0 10
    3
    40 60
    80 30
    80 60
    3
    0 40
    10 50
    0 60
    4
    30 -20
    40 -20
    40 10
    30 10
    
    Expected output
    2.828
    60.000
    
  2. Example 2

    Input
    1
    2
    1 2
    4
    0 0
    1 0
    1 1
    0 1
    4
    5 0
    6 0
    6 1
    5 1
    
    Expected output
    4.000