Problem:
Score : 600 points

Problem Statement

On the xy-plane, Snuke is going to travel from the point (x_s, y_s) to the point (x_t, y_t).
He can move in arbitrary directions with speed 1.
Here, we will consider him as a point without size.

There are N circular barriers deployed on the plane.
The center and the radius of the i-th barrier are (x_i, y_i) and r_i, respectively.
The barriers may overlap or contain each other.

A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.

Snuke wants to avoid exposure to cosmic rays as much as possible during the travel.
Find the minimum possible duration of time he is exposed to cosmic rays during the travel.

Constraints

All input values are integers.

-10^9 ≤ x_s, y_s, x_t, y_t ≤ 10^9

(x_s, y_s) ≠ (x_t, y_t)

1≤N≤1,000

-10^9 ≤ x_i, y_i ≤ 10^9

1 ≤ r_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

x_s y_s x_t y_t
N
x_1 y_1 r_1
x_2 y_2 r_2
:
x_N y_N r_N

Output

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel.
The output is considered correct if the absolute or relative error is at most 10^{-9}.

Sample Input 1

-2 -2 2 2
1
0 0 1

Sample Output 1

3.6568542495

An optimal route is as follows:

Sample Input 2

-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

0.0000000000

An optimal route is as follows:

Sample Input 3

4 -2 -2 4
3
0 0 2
4 0 1
0 4 1

Sample Output 3

4.0000000000

An optimal route is as follows:

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
	public Program() { }

	static void Main(string[] args)
	{
		new Program().prog();
	}
	Scanner cin;
	const int MOD = 1000000007;
	const int INF = int.MaxValue - 10;
	const long INFL = long.MaxValue - 10;
	const double EPS = 1e-14;
	const double PI = 3.1415926536;
	
	void prog()
	{
		cin = new Scanner();
		int[,] dir8 = new int[8, 2] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
		int[,] dir4 = new int[4, 2] { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };

		int xs = cin.nextInt();
		int ys = cin.nextInt();
		int xt = cin.nextInt();
		int yt = cin.nextInt();

		int N = cin.nextInt();
		double[,] dist = new double[N + 2, N + 2];
		int[] x = new int[N];
		int[] y = new int[N];
		int[] r = new int[N];
		for (int i = 0; i < N; i++)
		{
			x[i] = cin.nextInt();
			y[i] = cin.nextInt();
			r[i] = cin.nextInt();
		}
		for (int i = 0; i < N; i++)
		{
			dist[0, i + 1] = Math.Max(EPS, Math.Sqrt((x[i] - xs) * (x[i] - xs) + (y[i] - ys) * (y[i] - ys)) - r[i]);
			dist[i + 1, 0] = Math.Max(EPS, Math.Sqrt((x[i] - xs) * (x[i] - xs) + (y[i] - ys) * (y[i] - ys)) - r[i]);
			for (int j = 0; j < N; j++)
			{
				dist[i + 1, j + 1] = Math.Max(EPS, Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])) - r[i] - r[j]);
				dist[j + 1, i + 1] = Math.Max(EPS, Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])) - r[i] - r[j]);
			}
			dist[i + 1, N + 1] = Math.Max(EPS, Math.Sqrt((x[i] - xt) * (x[i] - xt) + (y[i] - yt) * (y[i] - yt)) - r[i]);
			dist[N + 1, i + 1] = Math.Max(EPS, Math.Sqrt((x[i] - xt) * (x[i] - xt) + (y[i] - yt) * (y[i] - yt)) - r[i]);
		}
		/*
		Console.WriteLine();
		for (int i = 0; i < N+2; i++)
		{
			for (int j = 0; j < N+2; j++)
			{
				Console.Write("{0} ",dist[i,j]);
			}
			Console.WriteLine();
		}
		Console.WriteLine();
		*/
		Console.WriteLine("{0:0.00000000000000}", Dijkstra(dist, 0, N + 1, N + 2));
	}

	// ダイクストラ法O(n^2)（startとgoalの距離を返す）
	double Dijkstra(double[,] adjacent, int start, int goal, int N)
	{
		bool[] visited = new bool[N];
		double[] dist = new double[N];
		int[] prev = new int[N];

		for (int i = 0; i < N; i++)
		{
			dist[i] = INF;
			prev[i] = -1;
		}
		dist[start] = 0;
		prev[start] = start;
		double min;
		int pos = start;
		while (true)
		{
			min = INF;
			int next = -1;
			visited[pos] = true;
			for (int i = 0; i < N; i++)
			{
				if (visited[i]) continue;
				if (adjacent[pos, i] != 0)
				{
					double d = dist[pos] + adjacent[pos, i];
					if (d < dist[i])
					{
						dist[i] = d;
						prev[i] = pos;
					}
				}
				if (dist[i] < min)
				{
					min = dist[i];
					next = i;
				}
			}
			if (next == goal) break;
			pos = next;
		}
		return dist[goal];
	}
}

class Scanner
{
	string[] s;
	int i;

	char[] cs = new char[] { ' ' };

	public Scanner()
	{
		s = new string[0];
		i = 0;
	}

	public string next()
	{
		if (i < s.Length) return s[i++];
		string st = Console.ReadLine();
		while (st == "") st = Console.ReadLine();
		s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
		i = 0;
		return next();
	}

	public int nextInt()
	{
		return int.Parse(next());
	}

	public long nextLong()
	{
		return long.Parse(next());
	}

	public double nextDouble()
	{
		return double.Parse(next());
	}
}