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

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly double INF = 1e100;

    public void Solve()
    {
        int SX = Reader.Int(), SY = Reader.Int();
        int GX = Reader.Int(), GY = Reader.Int();
        int N = Reader.Int();
        var Circles = new int[N + 2][];
        Circles[0] = new[] { SX, SY, 0 };
        for (int i = 1; i <= N; i++) Circles[i] = Reader.IntLine();
        Circles[N + 1] = new[] { GX, GY, 0 };
        N = N + 2;
        var dp = new double[N];
        for (int i = 1; i < N; i++) dp[i] = INF;
        var que = new Queue<int>();
        que.Enqueue(0);
        var inQue = new bool[N];
        var len = new double[N,N];

        for (int a = 0; a < N; a++) 
            for (int b = 0; b < N; b++) 
                len[a,b] = Len(Circles[a][0],Circles[a][1], Circles[a][2],
                               Circles[b][0],Circles[b][1], Circles[b][2]);
        while (que.Count > 0)
        {
            int a = que.Dequeue();
            inQue[a] = false;
            for (int b = 0; b < N; b++)
            {
                double cost = dp[a] + len[a, b];
                if (CheckMin(ref dp[b], cost) && !inQue[b])
                {
                    inQue[b] = true;
                    que.Enqueue(b);
                }
            }
        }

        Console.WriteLine(dp[N - 1]);
    }

    bool CheckMin(ref double a, double b) { if (b < a) { a = b; return true; } return false; }
    double Len(long x1, long y1, long r1, long x2, long y2, long r2)
    {
        return Math.Max(0, Dist(x1, y1, x2, y2) - r1 - r2);
    }
    double Dist(long x1, long y1, long x2, long y2)
    {
        return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}