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.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

	static double d(double x, double y, double x2, double y2) {
		double dx = x - x2;
		double dy = y - y2;
		return Math.sqrt(dx*dx + dy*dy);
	}

	static double[][][] undirectedGraph(int n, int[] v1, int[] v2, double[] cost) {
		int[] cnt = new int[n];
		for (int i : v1) cnt[i]++;
		for (int i : v2) cnt[i]++;

		double[][][] g = new double[n][][];
		for (int i=0; i<n; i++) g[i] = new double[cnt[i]][2];
		for (int i=0; i<v1.length; i++) {
			int s = v1[i];
			int t = v2[i];

			g[s][--cnt[s]][0] = t;
			g[s][cnt[s]][1] = cost[i];

			g[t][--cnt[t]][0] = s;
			g[t][cnt[t]][1] = cost[i];
		}

		return g;
	}

	static double[] dijkstra(double[][][] g, int source) {
		int n = g.length;

		final double[] d = new double[n];
		Arrays.fill(d, 1<<30);
		d[source] = 0;

		TreeSet<Integer> pQ = new TreeSet<Integer>(new Comparator<Integer>(){
			public int compare(Integer a, Integer b) {
				if (d[a] != d[b]) return Double.compare(d[a], d[b]);
				return a - b;
			}
		});
		pQ.add(source);

		while (!pQ.isEmpty()) {
			int cur = pQ.pollFirst();

			for (int i=0; i<g[cur].length; i++) {
				int next = (int)g[cur][i][0];
				double dist = d[cur] + g[cur][i][1];
				if (dist < d[next]) {
					pQ.remove(next);
					d[next] = dist;
					pQ.add(next);
				}
			}
		}

		return d;
	}

	static void solve() {
		int sx = in.nextInt();
		int sy = in.nextInt();
		int gx = in.nextInt();
		int gy = in.nextInt();
		int n = in.nextInt();
		double[][] p = new double[n][3];
		for (int i=0; i<n; i++) {
			for (int j=0; j<3; j++) {
				p[i][j] = in.nextDouble();
			}
		}

		ArrayList<Integer> s = new ArrayList<>();
		ArrayList<Integer> t = new ArrayList<>();
		ArrayList<Double> c = new ArrayList<>();
		for (int i=0; i<n; i++) {
			for (int j=i+1; j<n; j++) {
				s.add(i); t.add(j);
				double d = d(p[i][0], p[i][1], p[j][0], p[j][1]);
				c.add(Math.max(0, d - (p[i][2] + p[j][2])));
			}
		}

		for (int i=0; i<n; i++) {
			s.add(n); t.add(i);
			double d = d(p[i][0], p[i][1], sx, sy);
			c.add(Math.max(0, d - p[i][2]));

			s.add(n+1); t.add(i);
			d = d(p[i][0], p[i][1], gx, gy);
			c.add(Math.max(0, d - p[i][2]));
		}

		s.add(n); t.add(n+1);
		c.add(d(sx, sy, gx, gy));

		int m = s.size();
		int[] ss = new int[m];
		int[] tt = new int[m];
		double[] cc = new double[m];
		for (int i=0; i<m; i++) {
			ss[i] = s.get(i);
			tt[i] = t.get(i);
			cc[i] = c.get(i);
		}

		double[][][] g = undirectedGraph(n+2, ss, tt, cc);

		double[] d = dijkstra(g, n);
		dump(d);
		out.printf("%.12f\n",d[n+1]);
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.nanoTime();

		solve();
		out.flush();

		long end = System.nanoTime();
		dump((end - start) / 1000000 + " ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}