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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Math;

class Program{
    static int n;
    static Barrier[] nodes;
    static double[,] graph;
    static double e = 1e-15;

    static void Main(){
        string[] input = Console.ReadLine().Split();
        int xs = int.Parse(input[0]);
        int ys = int.Parse(input[1]);
        int xt = int.Parse(input[2]);
        int yt = int.Parse(input[3]);
        n = int.Parse(Console.ReadLine());
        nodes = new Barrier[n + 2]; //nodes[0] : start, nodes[n+1] : goal
        nodes[0] = new Barrier(0,xs,ys,0);
        for (int i = 1;i <= n;i++){
            input = Console.ReadLine().Split();
            long x = long.Parse(input[0]);
            long y = long.Parse(input[1]);
            long r = long.Parse(input[2]);
            nodes[i] = new Barrier(i,x,y,r);
        }
        nodes[n + 1] = new Barrier(n + 1,xt,yt,0);
        //グラフの作成
        graph = new double[n + 2,n + 2];
        for (int i = 0;i <= n + 1;i++){
            for (int j = 0;j <= n + 1;j++){
                double dis = Barrier.Distance(nodes[i],nodes[j]);
                graph[i,j] = dis;
                graph[j,i] = dis;
            }
        }
        //nodes[0]からダイクストラ法を用いる
        PriorityQueue<Barrier> Q = new PriorityQueue<Barrier>(nodes[0]);
        nodes[0].distance = 0;
        Q.Enqueue(nodes[0]);
        while (Q.Count != 0){
            var u = Q.Dequeue();
            for (int i = 0;i <= n + 1;i++){
                var alt = u.distance + graph[u.id,i];
                if (nodes[i].distance > alt + e){
                    nodes[i].distance = alt;
                    Q.Enqueue(nodes[i]);
                }
            }
        }
        //答えの出力
        Console.WriteLine(nodes[n + 1].distance);
    }
}

class Barrier : IComparer<Barrier>{ //バリア
    public int id;
    public long center_x;
    public long center_y;
    public long r;
    public double distance = double.MaxValue; //始点からの最短距離

    public Barrier(int id,long x,long y,long r){
        this.id = id;
        center_x = x;
        center_y = y;
        this.r = r;
    }

    /* 2つのバリアb1からb2の移動で宇宙船を浴びる時間 */
    public static double Distance(Barrier b1,Barrier b2){
        long x = (long)Pow(b1.center_x - b2.center_x,2.0);
        long y = (long)Pow(b1.center_y - b2.center_y,2.0);
        return Max(0,Pow(x + y,0.5) - (b1.r + b2.r));
    } 

    /* distance順にソートするための比較 */
    int IComparer<Barrier>.Compare(Barrier b1,Barrier b2){
        return (b1.distance < b2.distance) ? -1 : (b1.distance == b2.distance) ? 0 : 1;
    }
}

class PriorityQueue<T>{
    private readonly List<T> heap;
    private readonly Comparison<T> compare;
    private int size;
    public PriorityQueue() : this(Comparer<T>.Default) { }
    public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
    public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
    public PriorityQueue(int capacity, Comparison<T> comparison){
        this.heap = new List<T>(capacity);
        this.compare = comparison;
    }
    public void Enqueue(T item)
    {
        this.heap.Add(item);
        var i = size++;
        while (i > 0){
            var p = (i - 1) >> 1;
            if (compare(this.heap[p], item) <= 0)
                break;
            this.heap[i] = heap[p];
            i = p;
        }
        this.heap[i] = item;
 
    }
    public T Dequeue(){
        var ret = this.heap[0];
        var x = this.heap[--size];
        var i = 0;
        while ((i << 1) + 1 < size){
            var a = (i << 1) + 1;
            var b = (i << 1) + 2;
            if (b < size && compare(heap[b], heap[a]) < 0) a = b;
            if (compare(heap[a], x) >= 0)
                break;
            heap[i] = heap[a];
            i = a;
        }
        heap[i] = x;
        heap.RemoveAt(size);
        return ret;
    }
    public T Peek() { return heap[0]; }
    public int Count { get { return size; } }
    public bool Any() { return size > 0; }
}