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

#include <algorithm>    //STL ????
#include <bitset>     //STL ????
#include <cctype> //????
#include <cerrno>      //?????
#include <cfloat>     //?????
#include <ciso646> //?????????
#include <climits>     //?????????????
#include <clocale>     //???????
#include <cmath>      //??????
#include <complex>     //???
#include <csignal> //??????
#include <csetjmp> //??????
#include <cstdarg> //????????
#include <cstddef> //????
#include <cstdio>      //????/????
#include <cstdlib>     //?????????????
#include <cstring>     //?????
#include <ctime>      //?????????
#include <cwchar>      //????????/??
#include <cwctype>     //?????
#include <deque>      //STL ??????
#include <exception>    //?????
#include <fstream>     //????/??
#include <functional>   //STL ??????(?????)
#include <limits>      //????????????
#include <list>      //STL ??????
#include <locale> //???????
#include <map>       //STL ????
#include <memory> //STL????????????
#include<new> //??????
#include <numeric> //STL???????
#include <iomanip>     //?????/??
#include <ios>       //????/????
#include <iosfwd>     //??/???????????
#include <iostream>    //?????/??
#include <istream>     //?????
#include <iterator> //STL???
#include <ostream>     //?????
#include <queue>      //STL ????
#include <set>       //STL ????
#include <sstream>     //???????
#include <stack>      //STL ????
#include <stdexcept>    //?????
#include <streambuf>    //????/????
#include <string>     //????
#include <typeinfo> //????????
#include <utility>     //STL ?????
#include <valarray> //??????????
#include <vector>     //STL ??????
using namespace std;
const double INF=1e18;
const int N=1010;
int n;

struct Edge{
	int nxt,to;
	double dis;
}edge[N*N];
int head[N],edge_num;
void add(int f,int t,double d){
	edge[edge_num].nxt=head[f];
	edge[edge_num].dis=d;
	edge[edge_num].to=t;
	head[f]=edge_num++;
	return;
}

struct node{
	int x,y,r;
}a[N],s,t;
double cale_dis(node a,node b){
	return max(0.0,sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))-a.r-b.r);
}

struct nd{
	int id;
	double d;
	bool friend operator < (nd a,nd b){
		return a.d>b.d;
	}
}nw;
priority_queue <nd> q;

bool vis[N];
double dist[N];
void dij(int x){
	for (int i=1;i<=n+2;i++) dist[i]=INF;
	dist[x]=nw.d=0.0; nw.id=x;
	q.push(nw);
	while(!q.empty()){
		nw=q.top(); q.pop();
		int u=nw.id;
		if (vis[u]) continue;
		vis[u]=1;
		for (int i=head[u];~i;i=edge[i].nxt){
			int v=edge[i].to;
			if (vis[v]||dist[v]<=dist[u]+edge[i].dis) continue;
			dist[v]=dist[u]+edge[i].dis;
			nw.id=v; nw.d=dist[v];
			q.push(nw);
		}
	}
	return;
}

int main(){
	memset(head,-1,sizeof(head));
	scanf("%d%d%d%d",&s.x,&s.y,&t.x,&t.y); s.r=0.0; t.r=0.0;
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].r);
	add(n+1,n+2,cale_dis(s,t)); add(n+2,n+1,cale_dis(s,t));
	for (int i=1;i<=n;i++){
		add(n+1,i,cale_dis(s,a[i])); add(i,n+1,cale_dis(s,a[i]));
		add(i,n+2,cale_dis(t,a[i])); add(n+2,i,cale_dis(t,a[i])); 
		for (int j=1;j<=n;j++) add(i,j,cale_dis(a[i],a[j]));
	}
	dij(n+1);
	printf("%.10lf",dist[n+2]);
	return 0;
}