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: Accepted)

#include<iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
#include <list>
#include <map>
#include <cmath>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <climits>
#include <queue>
#include <stack>
#include <cstdio>

using namespace std;

const double EPS (1e-8);
const double PI = acos(-1.0);

template<class T>
struct Po{
	T x,y;
	Po() : x(0.0),y(0.0) {}
	Po(const T &x,const T &y) : x(x),y(y) {}
	Po operator+(const Po& a) const { return Po(x + a.x,y + a.y); }
	Po operator-(const Po& a) const { return Po(x - a.x,y - a.y); }
	Po operator*(const double& a) const { return Po(x * a, y * a); }
	Po operator/(const double& a) const { return Po(x / a, y / a); }
};

enum{ CCW = 1,CW = -1,ON = 0};

typedef Po<double> P;

double SQ(double x) {return x* x; }
double abs(const P& a) { return sqrt(SQ(a.x) + SQ(a.y)); }
double dot( const P& a,const P &b){ return a.x * b.x + a.y * b.y ; }
double cross( const P& a,const P &b){ return a.x * b.y - a.y * b.x ; }

P rot(const P& a, double theta){
	return P( a.x * cos(theta) - a.y * sin(theta) ,
		a.x * sin(theta) + a.y * cos(theta) );
}

int ccw(P a,P b,P c){
	b = b - a; c = c - a;
	double rdir = cross(b,c);
	double len = abs(b) * abs(c);
	if( rdir > EPS *len) return CCW;
	else if( rdir < -EPS * len) return CW;
	return ON;
}

bool intersect(P& a1,P& b1,P& a2, P& b2){
	if(max(a1.x,b1.x) + EPS < min(a2.x,b2.x)
	||max(a1.y,b1.y) + EPS < min(a2.y,b2.y)
	||max(a2.x,b2.x) + EPS < min(a1.x,b1.x)
	||max(a2.y,b2.y) + EPS < min(a1.y,b1.y) ) return false;
	return ccw(a1,b1,a2) * ccw(a1,b1,b2) <= 0
		&& ccw(a2,b2,a1) * ccw(a2,b2,b1) <= 0;
}

typedef pair<P,P> PD;

PD GetContact(const P& p, const P &q,double pr,double pq){
	P a = q-p;
	P b = rot(a,PI / 2);
	double c = abs(a);
	b = b / c;

	double s = (pr + pq + c) / 2;
	double men = sqrt(s * (s - pr) * (s - pq) * (s - c));
	double l = 2 * men / c;
	double lp = sqrt(SQ(pr) - SQ(l));
	
	a = a / c * lp;
	b = b * l;
	return PD(p + a + b,p + a - b);
}

int n;
P p[100];
double r[100];
double dp[202][202];

#define FOR(i,n) for(int i = 0; i < (n) ; i++)


int main(){

	while(cin>>n,n){
		vector<P> us;
		vector<PD> vpd;
		FOR(i,n) cin>>p[i].x >> p[i].y >> r[i];
		FOR(i,202) FOR(j,202) dp[i][j] = 1e50;
		us.push_back(p[0]);

		for (int i = 0; i < n - 1; i++){
			PD pd = GetContact(p[i],p[i+1],r[i],r[i+1]);
			us.push_back(pd.first);
			us.push_back(pd.second);
			vpd.push_back(pd);
		}
		us.push_back(p[n-1]);

		for(int i = 0; i < (int)us.size(); i++){
			for(int j = i + 1; j < (int)us.size(); j++){
				bool ok = true;
				int l  = (1 + i) / 2;
				int r = (1 + j) / 2 - 1;
				for (int k = l; k < r; k++){
					if(!intersect(us[i],us[j],vpd[k].first,vpd[k].second))
						ok = false;
				}
				if(!ok) continue;
				double dist = abs(us[i] - us[j]);
				dp[j][i] = dp[i][j] = dist;
			}
		}

		int a = (int)us.size();
		FOR(k,a) FOR(i,a) FOR(j,a) dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);

		printf("%.10lf\n",dp[0][a - 1]);

	}
	return 0;
}