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 5.4.1) (judge verdict: Runtime Error)

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

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

typedef struct point2dwithcircle{
  int x,y,r;
} point;

double distance(point *a,point *b){
  int dx=a->x-b->x;
  int dy=a->y-b->y;
  int64 d=(int64)dx*dx+(int64)dy*dy;
  int64 r=a->r+b->r;
  if(d<=r*r) return 0;
  return (double)(d-r*r)/(sqrt(d)+r);
}

void run(void){
  point s,t;
  scanf("%d%d%d%d",&s.x,&s.y,&t.x,&t.y);
  s.r=0;
  t.r=0;
  int n;
  scanf("%d",&n);
  point *array=(point *)malloc(sizeof(int)*(n+2));
  array[0]=s;
  array[n+1]=t;
  int i;
  for(i=1;i<=n;i++) scanf("%d%d%d",&array[i].x,&array[i].y,&array[i].r);
  double *dp=(double *)malloc(sizeof(double)*(n+2));
  int *used=(int *)malloc(sizeof(int)*(n+2));
  for(i=0;i<=n+1;i++){
    dp[i]=1e18;
    used[i]=0;
  }
  dp[0]=0;
  for(i=1;i<=n+1;i++){
    int index;
    for(index=0;used[index]!=0;index++);
    double min=dp[index];
    int j;
    for(j=index+1;j<=n+1;j++){
      if(!used[j] && dp[j]<min){
	min=dp[j];
	index=j;
      }
    }
    used[index]=1;
    for(j=0;j<=n+1;j++){
      dp[j]=MIN(dp[j],dp[index]+distance(array+index,array+j));
    }
  }
  printf("%.10lf\n",dp[n+1]);
}

int main(void){
  run();
  return 0;
}