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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Main 
{
    static class Scanner
    {
        BufferedReader br;
        StringTokenizer tk=new StringTokenizer("");
        public Scanner(InputStream is) 
        {
            br=new BufferedReader(new InputStreamReader(is));
        }
        public int nextInt() throws IOException
        {
            if(tk.hasMoreTokens())
                return Integer.parseInt(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextInt();
        }
        public long nextLong() throws IOException
        {
            if(tk.hasMoreTokens())
                return Long.parseLong(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextLong();
        }
        public String next() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return next();
        }
        public String nextLine() throws IOException
        {
            tk=new StringTokenizer("");
            return br.readLine();
        }
        public double nextDouble() throws IOException
        {
            if(tk.hasMoreTokens())
                return Double.parseDouble(tk.nextToken());
            tk=new StringTokenizer(br.readLine());
            return nextDouble();
        }
        public char nextChar() throws IOException
        {
            if(tk.hasMoreTokens())
                return (tk.nextToken().charAt(0));
            tk=new StringTokenizer(br.readLine());
            return nextChar();
        }
        public int[] nextIntArray(int n) throws IOException
        {
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArray(int n) throws IOException
        {
            long a[]=new long[n];
            for(int i=0;i<n;i++)
                a[i]=nextLong();
            return a;
        }
        public int[] nextIntArrayOneBased(int n) throws IOException
        {
            int a[]=new int[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextInt();
            return a;
        }
        public long[] nextLongArrayOneBased(int n) throws IOException
        {
            long a[]=new long[n+1];
            for(int i=1;i<=n;i++)
                a[i]=nextLong();
            return a;
        }
    
    
    }
    public static void main(String args[]) throws IOException
    {
        new Thread(null, new Runnable() {
            public void run() {
                try
                {
                    solve();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }, "1", 1 << 26).start();
        
    }
    static class Circle{
        long x,y,r;
        Circle(long a,long b,long c){
            x=a;y=b;r=c;
        }
    }
    static class DSU
    {
        int n;
        int p[],r[];
        DSU(int y)
        {
            n=y+10;
            p=new int[n];
            r=new int[n];
            for(int i=0;i<n;i++)
            {
                p[i]=i;
            }
        }
        int find(int x)
        {
            if(p[x]==x)
                return x;
            return p[x]=find(p[x]);
        }
        boolean union(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a==b)
                return false;
            if(r[a]<r[b])
                p[a]=b;
            else if(r[b]<r[a])
                p[b]=a;
            else
            {
                p[b]=a;
                r[a]++;
            }
            return true;
        }
            
    }
    static class Pair{
        int nd;
        double wt;
        Pair(int a, double b){
            nd=a;
            wt=b;
        }
    }
    static boolean intersect(Circle c1, Circle c2){
        return (c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y)<=(c1.r+c2.r)*(c1.r+c2.r);
    }
    static double distance(Circle c1, Circle c2){
        long d=(c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y);
        double dis=Math.sqrt(d);
        return dis-c1.r-c2.r;
    }
    static void solve() throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter out=new PrintWriter(System.out);
        ArrayList<Circle> c=new ArrayList<>();
        Circle stc=new Circle(in.nextInt(), in.nextInt(),0);
        Circle endc=new Circle(in.nextInt(), in.nextInt(), 0);
        c.add(stc);
        int n=in.nextInt();
        for(int i=0;i<n;i++){
            c.add(new Circle(in.nextInt(), in.nextInt(), in.nextInt()));
        }
        c.add(endc);
        DSU d=new DSU(n);
        for(int i=0;i<=n+1;i++)
            for(int j=i+1;j<=n+1;j++)
                if(intersect(c.get(i), c.get(j)))
                    d.union(i, j);
                
            
        ArrayList<Pair> g[]=new ArrayList[n+2];
        for(int i=0;i<=n+1;i++)
            g[i]=new ArrayList<>();
        
        for(int i=0;i<=n+1;i++)
            for(int j=i+1;j<=n+1;j++)
                if(d.find(j)!=d.find(i)){
                    double dis=distance(c.get(i), c.get(j));
                    g[d.find(i)].add(new Pair(d.find(j),dis));
                    g[d.find(j)].add(new Pair(d.find(i),dis));
                }
        
        double dis[]=new double[n+2];
        boolean vis[]=new boolean[n+2];
        PriorityQueue<Pair> pq=new PriorityQueue<>((a,b)->Double.compare(a.wt, b.wt));
        pq.add(new Pair(d.find(0),0));
        Arrays.fill(dis,Double.MAX_VALUE);
        dis[d.find(0)]=0;
        while(!pq.isEmpty()){
            Pair ndx=pq.poll();
            if(vis[ndx.nd])
                continue;
            vis[ndx.nd]=true;
            for(Pair edg:g[ndx.nd]){
                double next=ndx.wt+edg.wt;
                if(next<dis[edg.nd]){
                    dis[edg.nd]=next;
                    pq.add(new Pair(edg.nd, next));
                }
            }
            
        }
        out.printf("%.14f",dis[d.find(n+1)]);
        out.close();
        
    }
}