Problem:
Problem I: Wind Passages

Wind Corridor is a covered passageway where strong wind is always blowing. It is a long corridor of
width W, and there are several pillars in it. Each pillar is a right prism and its face is a polygon (not
necessarily convex).

In this problem, we consider two-dimensional space where the positive x-axis points the east and the
positive y-axis points the north. The passageway spans from the south to the north, and its length is
infinity. Specifically, it covers the area 0 ≤ x ≤ W. The outside of the passageway is filled with walls.
Each pillar is expressed as a polygon, and all the pillars are located within the corridor without conflicting
or touching each other.

Wind blows from the south side of the corridor to the north. For each second, w unit volume of air can
be flowed at most if the minimum width of the path of the wind is w. Note that the path may fork and
merge, but never overlaps with pillars and walls.

Your task in this problem is to write a program that calculates the maximum amount of air that can be
flowed through the corridor per second.

Input

The input consists of multiple datasets. Each dataset has the following format:

The first line of the input contains two integers W and N. W is the width of the corridor, and N is the
number of pillars. W and N satisfy the following condition: 1 ≤ W ≤ 104 and 0 ≤ N ≤ 200.

Then, N specifications of each pillar follow. Each specification starts with a line that contains a single
integer M, which is the number of the vertices of a polygon (3 ≤ M ≤ 40). The following M lines describe the shape of the polygon. The i-th line (1 ≤ i ≤ M) contains two integers xi and yi that denote the coordinate of the i-th vertex (0 < xi < W, 0 < yi < 104).

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, your program should print a line that contains the maximum amount of air flow per second, in unit
volume. The output may contain arbitrary number of digits after the decimal point, but the absolute error
must not exceed 10-6.

Sample Input

5 2
4
1 1
1 2
2 2
2 1
4
3 3
3 4
4 4
4 3
0 0

Output for the Sample Input

3.41421356

Solution in JAVA (judge verdict: Accepted)

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	
	private static final Scanner IN = new Scanner(System.in);
	
	private static final double INFINITE = 1e16;
	private static final double EPS = 1e-8;
	
	class Path implements Comparable<Path> {
		
		private double length;
		private int vertex;
		
		private Path(double length, int vertex) {
			
			this.length = length;
			this.vertex = vertex;
		}

		@Override
		public int compareTo(Path object) {
			
			return Double.compare(length, object.length);
		}
	}
	
	private final double[] shortest = new double[MAX_N];
	private final PriorityQueue<Path> que = new PriorityQueue<Path>(MAX_N * MAX_N);
	
	private void dijkstra() {
		
		que.clear();
		Arrays.fill(shortest, 0, n, INFINITE);
		
		shortest[n - 2] = 0;
		que.add(new Path(shortest[n - 2], n - 2));
		
		while (!que.isEmpty()) {
			
			Path path = que.remove();
			if (Math.abs(shortest[path.vertex] - path.length) > EPS) {
				
				continue;
			}
			if (path.vertex == n - 1) {
				
				break;
			}
			
			for (int i = 0; i < n; i ++) {
				
				double length =
					shortest[path.vertex] + distance[path.vertex][i];
				if (shortest[i] > length) {
					
					shortest[i] = length;
					que.add(new Path(shortest[i] ,i));
					
				}
			}
		}
	}
	
	private static final int X = 0, Y = 1;
	
	private static double absSq(double a[]) {
		
		return a[X] * a[X] + a[Y] * a[Y];
	}
	
	private static double abs(double a[]) {
		
		return Math.sqrt(absSq(a));
	}
	
	private static double[] subtractPoint(double a[], double b[]) {
		
		return new double[] {a[X] - b[X], a[Y] - b[Y]};
	}
	
	private static double innerProduct(double a[], double b[]) {
		
		return a[X] * b[X] + a[Y] * b[Y];
	}
	
	private static double outerProduct(double a[], double b[]) {
		
		return a[X] * b[Y] - a[Y] * b[X];
	}
	
	private static double distanceLinePoint(double a[][], double b[]) {
		
		double a1_a0[] = subtractPoint(a[1], a[0]);
		return Math.abs(outerProduct(a1_a0, subtractPoint(b, a[0]))) / abs(a1_a0);
	}
	
	private static double distanceSegmentPoint(double a[][], double b[]) {
		
		double b_a0[] = subtractPoint(b, a[0]);
		if (innerProduct(subtractPoint(a[1], a[0]), b_a0) < EPS) {
			
			return abs(b_a0);
		}
		
		double b_a1[] = subtractPoint(b, a[1]);
		if (innerProduct(subtractPoint(a[0], a[1]), b_a1) < EPS) {
			
			return abs(b_a1);
		}
		
		return distanceLinePoint(a, b);
	}
	
	private static double distanceSegment(double a[][], double b[][]) {
		
		double distance = distanceSegmentPoint(a, b[0]);
		distance = Math.min(distance, distanceSegmentPoint(a, b[1]));
		distance = Math.min(distance, distanceSegmentPoint(b, a[0]));
		distance = Math.min(distance, distanceSegmentPoint(b, a[1]));
		
		return distance;
	}
	
	private static double distanceConvexHull(double a[][], int aVertexN, double b[][], int bVertexN) {
		
		double distance = Double.MAX_VALUE;
		for (int i = 0; i < aVertexN; i ++) {
			
			for (int j = 0; j < bVertexN; j ++) {
				
				distance = Math.min(distance,
						distanceSegment(new double[][] {a[i], a[(i + 1) % aVertexN]},
								new double[][] {b[j], b[(j + 1) % bVertexN]}));
			}
		}
		
		return distance;
	}
	
	private final double[][] distance = new double[MAX_N][MAX_N];
	
	private void distance() {
		
		for (int i = 0; i < n; i ++) {
			
			for (int j = i + 1; j < n; j ++) {
				
				distance[i][j] = distance[j][i] =
					distanceConvexHull(polygon[i], m[i], polygon[j], m[j]);
			}
		}
	}
	
	private static final int MAX_N = 202, MAX_M = 40;
	
	private int w, n;
	private final int m[] = new int[MAX_N];
	private final double polygon[][][] = new double[MAX_N][MAX_M][2];
	
	private void run() {
		
		while(((w = IN.nextInt()) | (n = IN.nextInt())) != 0) {
			
			for (int i = 0; i < n; i ++) {
				
				m[i] = IN.nextInt();
				for (int j = 0; j < m[i]; j ++) {
					
					polygon[i][j][X] = IN.nextInt();
					polygon[i][j][Y] = IN.nextInt();
				}
			}
			
			m[n] = m[n + 1] = 2;
			polygon[n][0][X] = polygon[n][1][X] = 0;
			polygon[n + 1][0][X] = polygon[n + 1][1][X] = w;
			polygon[n][0][Y] = polygon[n + 1][0][Y] = - INFINITE;
			polygon[n][1][Y] = polygon[n + 1][1][Y] = INFINITE;
			n+=2;
			
			distance();
			
			dijkstra();
			
			System.out.println(shortest[n - 1]);
		}
	}
	
	public static void main(String[] args) {
		
		new Main().run();
	}
}