This page is still under construction.

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

Walking on Thin Ice

Time limit1sMemory limit128 MB

Summary
Find the shortest path from y=0 to y=W where only the given safe polygons cost nothing and all other river points cost their length.
Level

Medium7 of 10

Topics
Geometry, Graph, Shortest path
Solved
No attempts yet

Problem

You are trying to cross a frozen river. Unfortunately the ice is not of uniform thickness: in some places it is thick enough to be safe to cross, while in others it is not. Before crossing, you sent out a lightweight robot to map where it is and is not safe to cross. Now you want to determine the minimum distance you must travel over unsafe ice to reach the other side of the river.

Model the river in the 2-D plane as the region between two infinitely long parallel lines. The edge of the river on your starting side is the line y=0y = 0, and the opposite edge is the line y=Wy = W, where WW is the width of the river. The parts of the river that are safe to cross are given as simple polygons (each polygon does not intersect itself and has positive area). Every point of the river that does not lie in a safe area is unsafe to cross.

You may start crossing at any point on your side (y=0y = 0) and may finish at any point on the far side (y=Wy = W).

Input

The first line contains the number of data sets KK, followed by the KK data sets, each of the following form.

The first line of a data set contains the width of the river WW and the number of safe areas NN (1≤W≤1,000,0001 \le W \le 1{,}000{,}000, 1≤N≤1001 \le N \le 100). This is followed by NN lines. Line ii describes the ii-th safe area and begins with the number of vertices VV (3≤V≤203 \le V \le 20). This is followed on the same line by VV pairs of integers giving the xx and yy coordinates of the vertices of the safe area in clockwise order.

Every xx coordinate is between −1,000,000-1{,}000{,}000 and 1,000,0001{,}000{,}000, and every yy coordinate is between 00 and WW. Every safe area is non-self-intersecting and has positive area, and no two safe areas intersect or touch.

Output

For each data set, first output Data Set x: on a line by itself, where xx is its number (starting from 1). On the next line, output the minimum distance you must travel over unsafe ice to cross the river, rounded to two decimal places.

Output a single blank line between consecutive data sets (do not print a blank line after the last data set).

Examples3

  1. Example 1

    Input
    2
    100 1
    3 0 0 10 85 20 0
    10 2
    4 -2 0 -2 2 2 2 2 0
    4 -2 5 -2 8 2 8 2 5
    
    Expected output
    Data Set 1:
    15.00
    
    Data Set 2:
    5.00
    
  2. Example 2

    Input
    1
    10 1
    4 0 0 0 10 3 10 3 0
    
    Expected output
    Data Set 1:
    0.00
    
  3. Example 3

    Input
    1
    10 1
    4 0 4 0 6 2 6 2 4
    
    Expected output
    Data Set 1:
    8.00