Problem:
Problem E: The Two Men of the Japanese Alps

Two experienced climbers are planning a first-ever attempt: they start at two points of the equal altitudes on a mountain range, move back and forth on a single route keeping their altitudes equal, and finally meet with each other at a point on the route. A wise man told them that if a route has no point lower than the start points (of the equal altitudes) there is at least a way to achieve the attempt. This is the reason why the two climbers dare to start planning this fancy attempt.

The two climbers already obtained altimeters (devices that indicate altitude) and communication devices that are needed for keeping their altitudes equal. They also picked up a candidate route for the attempt: the route consists of consequent line segments without branches; the two starting points are at the two ends of the route; there is no point lower than the two starting points of the equal altitudes. An illustration of the route is given in Figure E.1 (this figure corresponds to the first dataset of the sample input).

Figure E.1: An illustration of a route

The attempt should be possible for the route as the wise man said. The two climbers, however, could not find a pair of move sequences to achieve the attempt, because they cannot keep their altitudes equal without a complex combination of both forward and backward moves. For example, for the route illustrated above: a climber starting at p1 (say A) moves to s, and the other climber (say B) moves from p6 to p5; then A moves back to t while B moves to p4; finally A arrives at p3 and at the same time B also arrives at p3. Things can be much more complicated and thus they asked you to write a program to find a pair of move sequences for them.

There may exist more than one possible pair of move sequences, and thus you are requested to find the pair of move sequences with the shortest length sum. Here, we measure the length along the route surface, i.e., an uphill path from (0, 0) to (3, 4) has the length of 5.

Input

The input is a sequence of datasets.

The first line of each dataset has an integer indicating the number of points N (2 ≤ N ≤ 100) on the route. Each of the following N lines has the coordinates (xi, yi) (i = 1, 2, ... , N) of the points: the two start points are (x1, y1) and (xN, yN); the line segments of the route connect (xi, yi) and (xi+1, yi+1) for i = 1, 2, ... , N - 1. Here, xi is the horizontal distance along the route from the start point x1, and yi is the altitude relative to the start point y1. All the coordinates are non-negative integers smaller than 1000, and inequality xi < xi+1 holds for i = 1, 2, .. , N - 1, and 0 = y1 = yN ≤ yi for i = 2, 3, ... , N - 1.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output the minimum sum of lengths (along the route) of move sequences until the two climbers meet with each other at a point on the route. Each output value may not have an error greater than 0.01.

Sample Input

6
0 0
3 4
9 12
17 6
21 9
33 0
5
0 0
10 0
20 0
30 0
40 0
10
0 0
1 2
3 0
6 3
9 0
11 2
13 0
15 2
16 2
18 0
7
0 0
150 997
300 1
450 999
600 2
750 998
900 0
0

Output for the Sample Input

52.5
40.0
30.3356209304689
10078.072814085803

Solution in JAVA (judge verdict: Memory Limit Exceeded)


import java.awt.geom.*;
import java.util.*;

public class Main {
	//0445 start
	//0536 cording end, last dataset is not matched
	//0538 stop and ready to go to school 
	
	int INF = 1 << 24;
	final double EPS = 1.0e-08;
	double EPS2 = 1.0e-02;
	int n, s1, s2;
	Point2D s1p, s2p;
	Point2D [] toy;
	
	class Edge{
		int from, to;
		double cost;
		public Edge(int from, int to, double cost) {
			this.from = from;
			this.to = to;
			this.cost = cost;
		}
		@Override
		public String toString() {
			return "Edge [from=" + from + ", to=" + to + ", cost=" + cost + "]";
		}
		
	}
	
	class C implements Comparable<C>{
		int ind;
		Point2D p;
		public C(int ind, Point2D p) {
			this.ind = ind;
			this.p = p;
		}
		@Override
		public int compareTo(C o) {
			if(this.p.getX() < o.p.getX()) return -1;
			else if(this.p.getX() > o.p.getX()) return 1;
			else if(this.p.getY() < o.p.getY()) return -1;
			else if(this.p.getY() > o.p.getY()) return 1;
			else return 0;
		}
	}
	Comparator< Point2D > com = new Comparator< Point2D >() {
		public int compare(Point2D o1, Point2D o2) {
			if(o1.getX() < o2.getX()) return -1;
			else if(o1.getX() > o2.getX()) return 1;
			else if(o1.getY() < o2.getY()) return -1;
			else if(o1.getY() > o2.getY()) return 1;
			else return 0;
		}
	};
	
	class State implements Comparable<State>{
		int now1, now2;
		double cost;
		public State(int now1, int now2, double cost) {
			super();
			this.now1 = now1;
			this.now2 = now2;
			this.cost = cost;
		}
		@Override
		public int compareTo(State o) {
			if(this.cost < o.cost ) return -1;
			if(this.cost > o.cost)	return 1;
			return 0;
		}
		
	}
	
	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(true){
			n = sc.nextInt();
			if(n == 0) break;
			Point2D [] plist = new Point2D[n];
			HashSet<Integer> ylist = new HashSet<Integer>();
			for(int i = 0 ; i < n; i ++){
				int x = sc.nextInt();
				int y = sc.nextInt();
				ylist.add(y);
				plist[i] = new Point2D.Double(x,y);
			}
			s1p = plist[0];
			s2p = plist[n-1];
			
			ArrayList<Line2D> llist = new ArrayList<Line2D>();
			for(int i = 1;i < n; i++){
				Line2D l = new Line2D.Double(plist[i-1], plist[i]);
				llist.add(l);
			}
			ArrayList<Line2D> l2list = new ArrayList<Line2D>();
			for(int y : ylist){
				Point2D p1 = new Point2D.Double(-1,y);
				Point2D p2 = new Point2D.Double(1001, y);
				Line2D ll = new Line2D.Double(p1, p2);
				l2list.add(ll);
			}
			
			ArrayList<ArrayList<Edge>> pass = segmentArrangement(llist, l2list);
			double ans = solve(pass);
			System.out.printf("%f\n",ans);
			
		}
		
	}
	
	private double solve(ArrayList<ArrayList<Edge>> pass) {
		double ans = INF;
		int passsize = pass.size();
		PriorityQueue<State> open = new PriorityQueue<State>();
		open.add(new State(s1, s2, 0.0));
		double [][] close = new double[passsize][passsize];
		for(int i = 0 ; i < passsize; i++){
			Arrays.fill(close[i], INF);
		}
		close[s1][s2] = 0.0;
		while(! open.isEmpty()){
			State now = open.poll();
			if(now.now1 == now.now2){
				ans = Math.min(ans, now.cost);
				break;
			}
			
			for(int i = 0 ; i < pass.get(now.now1).size(); i++){
				for(int j = 0; j < pass.get(now.now2).size(); j++){
					int next1 = pass.get(now.now1).get(i).to;
					int next2 = pass.get(now.now2).get(j).to;
					if(next1 == now.now1 && next2 == now.now2)continue;
					if(Math.abs(toy[next1].getY() - toy[next2].getY()) < EPS){
						double value = pass.get(now.now1).get(i).cost+ pass.get(now.now2).get(j).cost;
						double nextcost = now.cost + value;
						if(close[next1][next2] <= nextcost) continue;
						close[next1][next2] = nextcost;
						open.add(new State(next1, next2, nextcost));
					}
				}
			}
		}
		return ans;
	}

	private ArrayList<ArrayList<Edge>> segmentArrangement(ArrayList<Line2D> arg, ArrayList<Line2D> l2) {
		Line2D [] line = arg.toArray(new Line2D[arg.size()]);
		//交点リストを求める
		ArrayList<Point2D> intersectionlist = new ArrayList<Point2D>();
		for(int i = 0; i < line.length; i++){
			intersectionlist.add(line[i].getP1());
			intersectionlist.add(line[i].getP2());
			for(int j = i + 1; j < line.length; j++){
				if(line[i].intersectsLine(line[j])){
					Point2D p = intersectPtSS(line[i], line[j]);
					if(p == null) continue;
					intersectionlist.add(p);
				}
			}
		}
		
		for(int i = 0 ; i < l2.size(); i++){
			for(int j = 0; j < line.length; j++){
				if(l2.get(i).intersectsLine(line[j])){
					Point2D p = intersectPtSS(l2.get(i), line[j]);
					if(p == null) continue;
					intersectionlist.add(p);
				}
			}
		}

		//sortする。重複削除
		Collections.sort(intersectionlist, com);
		for(int i = 1; i < intersectionlist.size(); i++){
			if(issame(intersectionlist.get(i-1), intersectionlist.get(i))){
				intersectionlist.remove(i);
				i--;
			}
		}
		
		

		//交点リストから、線分を通過するもの同士でグラフを生成する。
		int len = intersectionlist.size();
		toy = new Point2D[len];
		for(int i = 0 ; i < len; i++){
			toy[i] = intersectionlist.get(i);
			if(issame(toy[i], s1p)){
				s1 = i;
			}
			if(issame(toy[i], s2p)){
				s2 = i;
			}
		}
		
		//double [][] res = new double[len][len];
		ArrayList<ArrayList<Edge>> res = new ArrayList<ArrayList<Edge>>();
		for(int i = 0; i < len; i++){
			res.add(new ArrayList<Main.Edge>());
			res.get(i).add(new Edge(i, i, 0.0));
		}
		for(int i = 0; i < line.length; i++){
			
			ArrayList<C> list = new ArrayList<C>();
			for(int j = 0; j < len; j++){
				if(line[i].ptSegDist(intersectionlist.get(j)) < EPS2){
					list.add(new C(j, intersectionlist.get(j)));
				}
			}
			Collections.sort(list);
			for(int j = 1; j < list.size(); j++){
				int from = list.get(j-1).ind;
				int to = list.get(j).ind;
				double cost = list.get(j).p.distance(list.get(j-1).p);
				res.get(from).add(new Edge(from, to, cost));
				res.get(to).add(new Edge(to, from, cost));
			}
		}
		return res;
	}
	
	private Point2D intersectPtSS(Line2D l, Line2D m) {
		Point2D lVec = sub(l.getP2(), l.getP1());
		Point2D mVec = sub(m.getP2(), m.getP1());
		Point2D m1l1Vec = sub(m.getP1(), l.getP1());
		double a = cross(m1l1Vec, lVec);
		double b = cross(lVec, mVec);
		if(Math.abs(a) < EPS && Math.abs(b) < EPS){
			//平行な直線同士の場合
			return null;
//			if(l.getP1().distance(m.getP1()) < EPS) return l.getP1();
//			if(l.getP1().distance(m.getP2()) < EPS) return l.getP1();
//			return l.getP2();
		}
		double t = a / b;
		double resx = m.getX1() + t * mVec.getX();
		double resy = m.getY1() + t * mVec.getY();
		return new Point2D.Double(resx, resy);
	}
	
	private boolean issame(Point2D p1, Point2D p2) {
		if(Math.abs(p1.getX() - p2.getX()) < EPS && Math.abs(p1.getY() - p2.getY()) < EPS ){
			return true;
		}
		return false;
	}
	private double cross(Point2D p1, Point2D p2) {
		double res = p1.getX() * p2.getY() - p1.getY() * p2.getX();
		return res;
	}
	private Point2D sub(Point2D p2, Point2D p1) {
		double x = p2.getX() - p1.getX();
		double y = p2.getY() - p1.getY();
		return new Point2D.Double(x, y);
	}

	public static void main(String [] args){
		new Main().doit();
	}
}