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: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import static java.lang.Math.sqrt;
import static java.lang.Math.max;
import static java.util.Arrays.fill;



public class Main {
    public static void main(String[] args) {
        FastScanner fsc = new FastScanner();
        long xs = fsc.nextLong();
        long ys = fsc.nextLong();
        long xt = fsc.nextLong();
        long yt = fsc.nextLong();
        int n = fsc.nextInt();
        long[][] xyr = new long[n + 2][3];
        xyr[0][0] = xs;
        xyr[0][1] = ys;
        xyr[0][2] = 0;
        for (int i = 1; i <= n; i++) {
            long x = fsc.nextLong();
            long y = fsc.nextLong();
            long r = fsc.nextLong();
            xyr[i][0] = x;
            xyr[i][1] = y;
            xyr[i][2] = r;
        }
        xyr[n + 1][0] = xt;
        xyr[n + 1][1] = yt;
        xyr[n + 1][2] = 0;
        ArrayList<Edge>[] edges = new ArrayList[n + 2];
        for (int i = 0; i <= n + 1; i++) {
            edges[i] = new ArrayList<>(n + 1);
        }
        for (int i = 0; i <= n + 1; i++) {
            for (int j = i + 1; j <= n + 1; j++) {
                long xsq = (xyr[i][0] - xyr[j][0]) * (xyr[i][0] - xyr[j][0]);
                long ysq = (xyr[i][1] - xyr[j][1]) * (xyr[i][1] - xyr[j][1]);
                double d = sqrt(xsq + ysq);
                double cost = max(d - xyr[i][2] - xyr[j][2], 0);
                edges[i].add(new Edge(i, j, cost));
                edges[j].add(new Edge(j, i, cost));
            }
        }
        System.out.println(dijkstra(edges, 0, n + 1));
    }

    public static double dijkstra(ArrayList<Edge>[] edges, int s, int t) {
        int n = edges.length;
        int[] prev = new int[n];
        double[] d = new double[n];
        PriorityQueue<Integer> pq = new PriorityQueue<>((u, v) -> Double.compare(d[u], d[v]));
        fill(d, 1e100);
        d[s] = 0;
        pq.add(s);
        while (pq.size() > 0) {
            int now = pq.poll();
            if (now == t) {
                break;
            }
            for (Edge e : edges[now]) {
                int next = e.to;
                double c = e.cost;
                if (d[now] + c < d[next]) {
                    d[next] = d[now] + c;
                    prev[next] = now;
                    pq.add(next);
                }
            }
        }
        return d[t];
    }

    static class Edge {
        public int from;
        public int to;
        public double cost;

        protected Edge(int from, int to, double cost) {
            this.from = from;
            this.to = to;
            this.cost = cost;
        }
    }
}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}