Problem:
Chain-Confined Path

There is a chain consisting of multiple circles on a plane.
The first (last) circle of the chain only intersects with the next (previous) circle,
and each intermediate circle intersects only with the two neighboring circles.

Your task is to find the shortest path that satisfies the following conditions.

The path connects the centers of the first circle and the last circle.

The path is confined in the chain, that is, all the points on the path are located within or on at least one of the circles.

Figure E-1 shows an example of such a chain and the corresponding shortest path.

Figure E-1: An example chain and the corresponding shortest path

Input

The input consists of multiple datasets.
Each dataset represents the shape of a chain in the following format.

n

x1 y1 r1

x2 y2 r2

...

xn yn rn

The first line of a dataset contains an integer n (3 ≤ n ≤ 100)
representing the number of the circles.
Each of the following n lines contains three integers separated by a single space.
(xi, yi)
and ri represent
the center position and the radius of the i-th circle Ci.
You can assume that 0 ≤ xi ≤ 1000,
0 ≤ yi ≤ 1000, and
1 ≤ ri ≤ 25.

You can assume that Ci and
Ci+1 (1 ≤ i ≤ n−1) intersect at two separate points.
When j ≥ i+2, Ci and Cj are apart and either of them does not contain the other.
In addition, you can assume that any circle does not contain the center of any other circle.

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

Figure E-1 corresponds to the first dataset of Sample Input below.
Figure E-2 shows the shortest paths for the subsequent datasets of Sample Input.

Figure E-2: Example chains and the corresponding shortest paths

Output

For each dataset, output a single line containing the length of the shortest chain-confined path between the centers of the first circle and the last circle.
The value should not have an error greater than 0.001.
No extra characters should appear in the output.

Sample Input

10
802 0 10
814 0 4
820 1 4
826 1 4
832 3 5
838 5 5
845 7 3
849 10 3
853 14 4
857 18 3
3
0 0 5
8 0 5
8 8 5
3
0 0 5
7 3 6
16 0 5
9
0 3 5
8 0 8
19 2 8
23 14 6
23 21 6
23 28 6
19 40 8
8 42 8
0 39 5
11
0 0 5
8 0 5
18 8 10
8 16 5
0 16 5
0 24 5
3 32 5
10 32 5
17 28 8
27 25 3
30 18 5
0

Output for the Sample Input

58.953437
11.414214
16.0
61.874812
63.195179

Solution in C++ (judge verdict: Wrong Answer)

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
using namespace std;
#define EPS 1e-5
#define INF 1e9

double add(double a,double b){
	if(fabs(a+b)<EPS*(fabs(a)+fabs(b)))return 0;
	return a+b;
}

class P{
public:
	double x,y,r;
	P(){}
	P(double xx,double yy,double rr){
		x=xx;
		y=yy;
		r=rr;
	}
	P operator +(P p){
		return P(add(x,p.x),add(y,p.y),r);
	}
	P operator -(P p){
		return P(add(x,-p.x),add(y,-p.y),r);
	}
	P operator *(double d){
		return P(x*d,y*d,r);
	}
	P operator /(double d){
		return P(x/d,y/d,r);
	}
	double dot(P p){
		return add(x*p.x,y*p.y);
	}
	double det(P p){
		return add(x*p.y,-y*p.x);
	}
};

bool on_seg(P p1,P p2,P q){
	return (p1-q).det(p2-q)==0 && (p1-q).dot(p2-q)<=0;
}

P intersection(P p1,P p2,P q1,P q2){
	return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

class node{
public:
	double cost;
	int v,v2;
	node(){}
	node(double cc,int vv,int vv2){
		cost=cc;
		v=vv;
		v2=vv2;
	}
	bool operator< (const node& n1)const{
		return cost>n1.cost;
	}
};

int n;
P p[105];
P data[105][2];
double dp[105][2];

double dijk(){
	for(int i=0;i<=n;i++){
		dp[i][0]=dp[i][1]=INF;
	}
	priority_queue<node> que;
	que.push(node(0.0,0,0));
	dp[0][0]=0.0;
	while(que.size()){
		node q=que.top();
		que.pop();
		if(dp[q.v][q.v2]<q.cost)continue;
		for(int next=q.v;next<=n;next++){
			for(int j=0;j<2;j++){
				if((next==n+1 || next==0) && j==1)continue;
				bool flag=true;
				for(int k=q.v+1;k<next;k++){
					if((data[q.v][q.v2]-data[k][0]).det(data[next][j]-data[k][1])==0){
						if(!(on_seg(data[q.v][q.v2],data[k][0],data[next][j])|| on_seg(data[q.v][q.v2],data[k][0],data[k][1]) || on_seg(data[next][j],data[k][1],data[q.v][q.v2])|| on_seg(data[q.v][q.v2],data[k][1],data[k][0])))flag=false;
					}else{
						P r=intersection(data[q.v][q.v2],data[next][j],data[k][0],data[k][1]);
						if(!(on_seg(data[k][0],data[k][1],r) && on_seg(data[q.v][q.v2],data[next][j],r)))flag=false;
					}
				}
				if(flag){
					P len=data[next][j]-data[q.v][q.v2];
					double len2=sqrt((len.x*len.x)+(len.y*len.y));
					if(dp[q.v][q.v2]+len2<dp[next][j]){
						dp[next][j]=dp[q.v][q.v2]+len2;
						que.push(node(dp[next][j],next,j));
					}
				}
			}
		}
	}
	return dp[n][0];
}

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%lf %lf %lf",&p[i].x,&p[i].y,&p[i].r);
		}
		data[0][0]=p[0];
		data[n][0]=p[n-1];
		for(int i=0;i<n-1;i++){
			double len=(p[i].x-p[i+1].x)*(p[i].x-p[i+1].x)+(p[i].y-p[i+1].y)*(p[i].y-p[i+1].y);
			double d=(p[i].r*p[i].r-p[i+1].r*p[i+1].r+len)/(2*sqrt(len));
			double c=sqrt(p[i].r*p[i].r-d*d);
			P pp=p[i+1]-p[i];
			double ang=atan2(pp.y,pp.x);
			double ang2=atan2(c,d);
			data[i+1][0]=p[i]+P(p[i].r*cos(ang+ang2),p[i].r*sin(ang+ang2),0);
			data[i+1][1]=p[i]+P(p[i].r*cos(ang-ang2),p[i].r*sin(ang-ang2),0);
		}
		printf("%f\n",dijk());
	}
	return 0;
}