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 (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>
#include <math.h>

typedef struct {
	int id;
	long double key;
} data;
 
typedef struct {
	data obj[1000001];
	int size;
} min_heap;
 
void push(data x, min_heap* h)
{
	int i = h->size;
	data tmp;
	h->obj[(h->size)++] = x;
	while (1) {
		if (i == 0) break;
		else if (h->obj[i].key < h->obj[(i-1)/2].key) {
			tmp = h->obj[(i-1)/2];
			h->obj[(i-1)/2] = h->obj[i];
			h->obj[i] = tmp;
			i = (i - 1) / 2;
		} else break;
	}
}
 
data pop(min_heap* h)
{
	int i = 0, j;
	data output = h->obj[0], tmp;
	h->obj[0] = h->obj[--(h->size)];
	while (1) {
		if (i * 2 + 1 >= h->size) break;
		else if (i * 2 + 2 >= h->size) j = i * 2 + 1;
		else if (h->obj[i*2+1].key < h->obj[i*2+2].key) j = i * 2 + 1;
		else j = i * 2 + 2;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
		} else break;
	}
	return output;
}

int main()
{
	int i, N, s[2], t[2], x[1001], y[1001], r[1002];
	scanf("%d %d %d %d", &(s[0]), &(s[1]), &(t[0]), &(t[1]));
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d %d %d", &(x[i]), &(y[i]), &(r[i]));
	
	int j;
	long double tmp, time[1002][1002] = {};
	for (i = 1; i < N; i++) {
		for (j = i + 1; j <= N; j++) {
			tmp = sqrt((long double)(x[i] - x[j]) * (x[i] - x[j]) + (long double)(y[i] - y[j]) * (y[i] - y[j]));
			if (tmp < (long double)(r[i] + r[j])) tmp = 0.0;
			else tmp -= (long double)(r[i] + r[j]);
			time[i][j] = tmp;
			time[j][i] = tmp;
		}
	}
	for (i = 1; i <= N; i++) {
		tmp = sqrt((long double)(x[i] - s[0]) * (x[i] - s[0]) + (long double)(y[i] - s[1]) * (y[i] - s[1]));
		if (tmp < (long double)r[i]) tmp = 0.0;
		else tmp -= (long double)r[i];
		time[i][0] = tmp;
		time[0][i] = tmp;
		tmp = sqrt((long double)(x[i] - t[0]) * (x[i] - t[0]) + (long double)(y[i] - t[1]) * (y[i] - t[1]));
		if (tmp < (long double)r[i]) tmp = 0.0;
		else tmp -= (long double)r[i];
		time[i][N+1] = tmp;
		time[N+1][i] = tmp;
	}
	time[0][N+1] = sqrt((long double)(s[0] - t[0]) * (s[0] - t[0]) + (long double)(s[1] - t[1]) * (s[1] - t[1]));
	time[N+1][0] = time[0][N+1];
	
	int u, w, flag[1002] = {};
	long double min[1002];
	for (i = 1; i <= N + 1; i++) min[i] = 1e+100;
	data d;
	min_heap h;
	h.size = 0;
	d.key = 0.0;
	d.id = 0;
	push(d, &h);
	while (h.size > 0) {
		u = pop(&h).id;
		if (flag[u] != 0) continue;
		else if (u == N + 1) break;
		flag[u] = 1;
		for (w = 1; w <= N + 1; w++) {
			if (min[u] + time[u][w] < min[w]) {
				min[w] = min[u] + time[u][w];
				d.key = min[w];
				d.id = w;
				push(d, &h);
			}
		}
	}
	
	printf("%.10Lf\n", min[N+1]);
	fflush(stdout);
	return 0;
}