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

namespace ARC064C{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var Xs = sr.Next<int>();
            var Ys = sr.Next<int>();
            var Xt = sr.Next<int>();
            var Yt = sr.Next<int>();

            var N = sr.Next<int>();
            var O = sr.Next<int>(N, 3).Select(a => new{X = a[0], Y = a[1], R = a[2]}).ToArray();

            Func<long, long, long, long, double> dist = (x1, y1, x2, y2) => Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            var dj = new Dijkstra(N + 2);

            for(var i = 0; i < N; i++){
                for(var j = i + 1; j < N; j++){
                    dj.AddUndirectedEdge(i, j, Math.Max(0, (dist(O[i].X, O[i].Y, O[j].X, O[j].Y) - (O[i].R + O[j].R)) / 10.0));
                }
            }

            for(var i = 0; i < N; i++){
                dj.AddUndirectedEdge(i, N, Math.Max(0, (dist(O[i].X, O[i].Y, Xs, Ys) - O[i].R) / 10.0));
                dj.AddUndirectedEdge(i, N + 1, Math.Max(0, (dist(O[i].X, O[i].Y, Xt, Yt) - O[i].R) / 10.0));
            }

            dj.AddUndirectedEdge(N, N + 1, (dist(Xs, Ys, Xt, Yt)) / 10.0);
            Console.WriteLine(dj.Solve(N)[N + 1] * 10);
            //---------------------------------
        }
    }

    public class Dijkstra{
        public class Edge{
            public int To;
            public double Cost;
        }

        public class Node : IComparable<Node>{
            public int Id;
            public double Cost;

            public int CompareTo(Node node){
                return Math.Sign(Cost - node.Cost);
            }
        }

        private readonly List<Edge>[] _adj;

        public Dijkstra(int v){
            _adj = new List<Edge>[v];
            for(var i = 0; i < v; i++) _adj[i] = new List<Edge>();
        }

        public void AddEdge(int from, int to, double cost){
            _adj[from].Add(new Edge{To = to, Cost = cost});
        }

        public void AddUndirectedEdge(int from, int to, double cost){
            _adj[from].Add(new Edge{To = to, Cost = cost});
            _adj[to].Add(new Edge{To = from, Cost = cost});
        }

        public double[] Solve(int start){
            var v = _adj.Length;
            var dist = new double[v];

            for(var i = 0; i < v; i++) dist[i] = double.MaxValue;
            dist[start] = 0;

            var pq = new PriorityQueue<Node>();
            pq.Enqueue(new Node{Id = start, Cost = 0});

            while(pq.Count > 0){
                var node = pq.Dequeue();
                var from = node.Id;

                foreach(var edge in _adj[from]){
                    var to = edge.To;
                    var cost = node.Cost + edge.Cost;

                    if(cost >= dist[to]) continue;
                    dist[to] = cost;
                    pq.Enqueue(new Node{Id = to, Cost = cost});
                }
            }
            return dist;
        }
    }

    public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>{
        private readonly List<T> _list = new List<T>();
        public int Count => _list.Count;

        IEnumerator<T> IEnumerable<T>.GetEnumerator(){
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator(){
            return _list.GetEnumerator();
        }

        public void Clear(){
            _list.Clear();
        }

        public bool Contains(T value){
            return _list.Contains(value);
        }

        public T Peek(){
            return _list[0];
        }

        public void Enqueue(T value){
            _list.Add(value);
            PushHeap();
        }

        public T Dequeue(){
            var ret = _list[0];
            _list[0] = _list[Count - 1];
            _list.RemoveAt(Count - 1);
            PopHeap();
            return ret;
        }

        private void PushHeap(){
            var i = Count - 1;
            while(i != 0){
                var p = (i - 1) / 2;
                if(_list[i].CompareTo(_list[p]) > 0) return;

                SwapIndex(i, i = p);
            }
        }

        private void PopHeap(){
            var i = 0;
            while(true){
                var l = 2 * i + 1;
                var r = l + 1;

                var maxi = i;
                if(l < Count && _list[maxi].CompareTo(_list[l]) > 0) maxi = l;
                if(r < Count && _list[maxi].CompareTo(_list[r]) > 0) maxi = r;
                if(maxi == i) return;

                SwapIndex(i, i = maxi);
            }
        }

        private void SwapIndex(int left, int right){
            var tmp = _list[left];
            _list[left] = _list[right];
            _list[right] = tmp;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}