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;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;

class Myon
{
    static Scanner cin;
    public Myon() { }
    public static int Main()
    {
        //Console.SetOut(new Printer(Console.OpenStandardOutput()));
        cin = new Scanner();
        new Myon().calc();
        return 0;
    }


    public void calc()
    {
        double xs, ys, xt, yt;
        xs = cin.nextDouble();
        ys = cin.nextDouble();
        xt = cin.nextDouble();
        yt = cin.nextDouble();
        double ans = double.MaxValue;
        int N = cin.nextInt();
        bool[] used = new bool[N];
        double[] dist = new double[N];
        double[] x = new double[N];
        double[] y = new double[N];
        double[] r = new double[N];
        for (int i = 0; i < N; i++)
        {
            x[i] = cin.nextDouble();
            y[i] = cin.nextDouble();
            r[i] = cin.nextDouble();
        }


        for (int i = 0; i < N; i++)
        {
            dist[i] = hypot(xs - x[i], ys - y[i]) - r[i];
            if (dist[i] < 0) dist[i] = 0;
        }

        for (int i = 0; i < N; i++)
        {
            int best = -1;
            double bestdist = double.MaxValue;
            for (int j = 0; j < N; j++)
            {
                if(!used[j] && bestdist > dist[j])
                {
                    best = j;
                    bestdist = dist[j];
                }
            }
            int start = best;
            for (int j = 0; j < N; j++)
            {
                double add = hypot(x[start] - x[j], y[start] - y[j]) - r[start] - r[j];
                if (add < 0) add = 0;
                dist[j] = Math.Min(dist[j], dist[start] + add);
            }
            used[start] = true;
            ans = Math.Min(ans, dist[start] + Math.Max(0, hypot(xt - x[start], yt - y[start]) - r[start]));
        }
        Console.WriteLine("{0:0.0000000000000000}",ans);
    }

    double hypot(double x, double y)
    {
        return Math.Sqrt(x * x + y * y);
    }


}

class Printer : StreamWriter
{
    public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
    public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
    public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { }
}

class Scanner
{
    string[] s;
    int i;

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

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

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

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

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

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

}