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.Text;
using System.Collections.Generic;
class Solve{
    int N;
    long[] x;
    long[] y;
    long[] r;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Read();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        double[] d = new double[(N+2)*(N+2)];
        int[] f = new int[(N+2)*(N+2)];
        int[] t = new int[(N+2)*(N+2)];
        for(int i=0;i<N+2;i++){
            for(int j=0;j<N+2;j++){
                d[i*(N+2)+j] = Distance(i,j);
                f[i*(N+2)+j] = i;
                t[i*(N+2)+j] = j;
            }
        }
        Dijkstra D = new Dijkstra(f,t,d,N+2,N);
        sb.Append(D.GetCost(N+1)+"\n");

    }
    double Distance(int i,int j){
        return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) > (r[i]+r[j])*(r[i]+r[j]) ? Math.Sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])) - (r[i]+r[j]) : 0;
    }
    void Read(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(Console.ReadLine());
        x = new long[N+2];
        y = new long[N+2];
        r = new long[N+2];
        x[N] = Int64.Parse(str[0]);
        x[N+1] = Int64.Parse(str[2]);
        y[N] = Int64.Parse(str[1]);
        y[N+1] = Int64.Parse(str[3]);
        r[N] = 0;
        r[N+1] = 0;
        for(int i=0;i<N;i++){
            str = Console.ReadLine().Split(' ');
            x[i] = Int64.Parse(str[0]);
            y[i] = Int64.Parse(str[1]);
            r[i] = Int64.Parse(str[2]);
        }
    }    
}
struct Edge{
    public Vertex v;
    public double cost;
    public Edge(Vertex v0,double c){
        v = v0;
        cost = c;
    }
}
class Vertex{
    public bool finished;
    public List<Vertex> edges;
    public List<double> costs;
    public double cost;
    public void AddEdge(Vertex v,double c){
        edges.Add(v);
        costs.Add(c);
    }
    public Vertex(){
        edges = new List<Vertex>();
        costs = new List<double>();
        cost = -1;
        finished = false;
    }
}
class Dijkstra{
	int E;
	Vertex[] point;
    Vertex S;
    public double GetCost(int i){
        return point[i].cost;
    }
    public Dijkstra(int[] f,int[] t,double[] cost,int n,int s){
        point = new Vertex[n];
        for(int i=0;i<n;i++){
            point[i] = new Vertex();
        }
        E = f.Length;
        for(int i=0;i<E;i++){
            point[f[i]].AddEdge(point[t[i]],cost[i]);
        }
        S = point[s];
        calc();
    }
    void calc(){
        Heap H = new Heap(2*E);
        S.cost = 0;
        H.push(new Edge(S,0));
        while(!H.Empty()){
            Vertex v = H.pop().v;
            if(v.finished){
                continue;
            }
            v.finished = true;
            for(int i=0;i<v.edges.Count;i++){
                Vertex vi = v.edges[i];
                if(vi.cost == -1 || vi.cost > v.costs[i]+v.cost){
                    vi.cost = v.costs[i]+v.cost;
                    H.push(new Edge(vi,vi.cost));
                }
            }
        }
    }
}
class Heap{
    public int size;
    Edge[] obj;
    public Heap(int maxsize){
        size = 0;
        obj = new Edge[maxsize];
    }
    public void push(Edge a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(obj[p].cost > a.cost){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public bool Empty(){
        return size == 0;
    }
    public Edge pop(){
        Edge t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && obj[p+1].cost < obj[p].cost){
                p++;
            }
            if(obj[p].cost < obj[size].cost){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
}