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.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Main {
	public static class Node {
		int before;
		double cost;
		public Node() {
			this.before = -1;
			this.cost = Double.MAX_VALUE;
		}
	}

	public static Node[] dijkstra(int n, int start, double[][] matrix) {
		boolean[] defnode = new boolean[n];
		Node[] cost = new Node[n];
		for(int i=0; i<n; i++) {
			defnode[i] = false;
			cost[i] = new Node();
		}
		cost[start].cost = 0;
		int node = start;
		int counter = 0;
		while(true) {
			counter++;
			if(counter == n || node == Integer.MAX_VALUE) return cost;
			defnode[node] = true;
			for(int i=0; i<n; i++) {
				if(defnode[i]) continue;
				if(matrix[node][i] == Double.MAX_VALUE) continue;
				if((cost[node].cost + matrix[node][i]) < cost[i].cost) {
					cost[i].cost = cost[node].cost + matrix[node][i];
					cost[i].before = node;
				}
			}
			int minnode = Integer.MAX_VALUE;
			Double mincost = Double.MAX_VALUE;
			for(int i=0; i<n; i++) {
				if(defnode[i]) continue;
				if(cost[i].cost < mincost) {
					minnode = i;
					mincost = cost[i].cost;
				}
			}
			node = minnode;
		}
	}

	public static void main(String[] args) {
		FS fs = new FS();
		// start
		int xs = fs.nextInt();
		int ys = fs.nextInt();
		// goal
		int xt = fs.nextInt();
		int yt = fs.nextInt();
		// num of barrier
		int N = fs.nextInt();
		double[][] matrix = new double[N+2][N+2];
		int[] x = new int[N+2];
		int[] y = new int[N+2];
		int[] r = new int[N+2];
		x[0] = xs;
		y[0] = ys;
		r[0] = 0;
		x[N+1] = xt;
		y[N+1] = yt;
		r[N+1] = 0;
		for(int i=0; i<N; i++) {
			x[i+1] = fs.nextInt();
			y[i+1] = fs.nextInt();
			r[i+1] = fs.nextInt();
		}
		for(int i=0; i<N+2; i++) {
			matrix[i][i] = Double.MAX_VALUE;
		}

		// calculate cost
		for(int i=0; i<N+1; i++) {
			for(int j=i+1; j<N+2; j++) {
				double xi = (double)x[i];
				double xj = (double)x[j];
				double yi = (double)y[i];
				double yj = (double)y[j];
				double ri = (double)r[i];
				double rj = (double)r[j];
				double l = Math.max(0.0, Math.sqrt(Math.pow(xi-xj, 2.0)+Math.pow(yi-yj, 2.0))-ri-rj);
				matrix[i][j] = l;
				matrix[j][i] = l;
			}
		}

		Node[] cost = dijkstra(N+2, 0, matrix);
		System.out.println(cost[N+1].cost);
	}

	// Read Class
	static class FS {
		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() { return hasNextByte() ? buffer[ptr++] : -1;}
		private boolean isPrintableChar(int c) {return 33 <= c && c <= 126;}
		private void skipUnprintable() {while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}

		public boolean hasNext() { skipUnprintable(); 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 int nextInt() {
			return (int)nextLong();
		}
		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();
			}
		}
	}
}