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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 100000000;
using namespace std;


typedef complex<double> Point;
typedef Point Vector;
//線分を表す構造体
struct Segment{ Point p1, p2; };
//直線を表す構造体
typedef Segment Line;
//多角形を表す構造体
typedef vector<Point> Polygon;

namespace std{
    bool operator < (const Point &a, const Point &b){
        return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
    }
    bool operator == (const Point &a, const Point &b){
        return a.real() == b.real() && a.imag() == b.imag();
    }
}

class Circle{
    public:
        Point c;
        double r;
        Circle(Point c = Point(), double r = 0.0): c(c), r(r) {}
};

// 許容する誤差
#define EPS (1e-10)

// ベクトルaの絶対値を求める
//double length = abs(a);

// 2点a,b間の距離を求める
//double distance = abs(a-b);

/*
// ベクトルaの単位ベクトルを求める
Point b = a / abs(a);

// ベクトルaの法線ベクトルn1,n2を求める
Point n1 = a * Point(0, 1);
Point n2 = a * Point(0, -1);
*/

int ccw(Point, Point, Point);

// 2つのスカラーが等しいかどうか
bool EQ(double a, double b){
    return (abs(a - b) < EPS);
}

// 2つのベクトルが等しいかどうか
bool EQV(Vector a, Vector b){
    return ( EQ(a.real(), b.real()) && EQ(a.imag(), b.imag()) );
}

// 内積 (dot product) : a・b = |a||b|cosΘ
double dot(Point a, Point b) {
    return (a.real() * b.real() + a.imag() * b.imag());
}

// 外積 (cross product) : a×b = |a||b|sinΘ
double cross(Point a, Point b) {
    return (a.real() * b.imag() - a.imag() * b.real());
}

// 2直線の直交判定 : a⊥b <=> dot(a, b) = 0
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) {
    return EQ( dot(a1-a2, b1-b2), 0.0 );
}
bool isOrthogonal(Line s1, Line s2) {
    return isOrthogonal(s1.p1, s1.p2, s2.p1, s2.p2);
}

// 2直線の平行判定 : a//b <=> cross(a, b) = 0
bool isParallel(Point a1, Point a2, Point b1, Point b2) {
    return EQ( cross(a1-a2, b1-b2), 0.0 );
}
bool isParallel(Line s1, Line s2) {
    return isParallel(s1.p1, s1.p2, s2.p1, s2.p2);
}

// 点cが直線a,b上にあるかないか
bool isPointOnLine(Point a, Point b, Point c) {
    return EQ( cross(b-a, c-a), 0.0 );
}
bool isPointOnLine(Line s, Point c) {
    return isPointOnLine(s.p1, s.p2, c);
}

// 点a,bを通る直線と点cとの距離
double distanceLPoint(Point a, Point b, Point c) {
    return abs(cross(b-a, c-a)) / abs(b-a);
}
double distanceLPoint(Line s, Point c) {
    return distanceLPoint(s.p1, s.p2, c);
}

// 点a,bを端点とする線分と点cとの距離
double distanceLsPoint(Point a, Point b, Point c) {
    if ( dot(b-a, c-a) < EPS ) return abs(c-a);
    if ( dot(a-b, c-b) < EPS ) return abs(c-b);
    return abs(cross(b-a, c-a)) / abs(b-a);
}
double distanceLsPoint(Segment s, Point c) {
    return distanceLsPoint(s.p1, s.p2, c);
}

// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
bool isIntersectedLs(Point a1, Point a2, Point b1, Point b2) {
    return ( ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
            ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0 );
}
bool isIntersectedLs(Segment s1, Segment s2) {
    return isIntersectedLs(s1.p1, s1.p2, s2.p1, s2.p2);
}

// a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算
Point intersectionLs(Point a1, Point a2, Point b1, Point b2) {
    Vector base = b2 - b1;
    double d1 = abs(cross(base, a1 - b1));
    double d2 = abs(cross(base, a2 - b1));
    double t = d1 / (d1 + d2);

    return Point(a1 + (a2 - a1) * t);
}
Point intersectionLs(Segment s1, Segment s2) {
    return intersectionLs(s1.p1, s1.p2, s2.p1, s2.p2);
}

// a1,a2を通る直線とb1,b2を通る直線の交差判定
bool isIntersectedL(Point a1, Point a2, Point b1, Point b2) {
    return !EQ( cross(a1-a2, b1-b2), 0.0 );
}
bool isIntersectedL(Line l1, Line l2) {
    return isIntersectedL(l1.p1, l1.p2, l2.p1, l2.p2);
}

// a1,a2を通る直線とb1,b2を通る直線の交点計算
Point intersectionL(Point a1, Point a2, Point b1, Point b2) {
    Point a = a2 - a1; Point b = b2 - b1;
    return a1 + a * cross(b, b1-a1) / cross(b, a);
}
Point intersectionL(Line l1, Line l2) {
    return intersectionL(l1.p1, l1.p2, l2.p1, l2.p2);
}

// 線分s1と線分s2の距離
double distanceLL(Segment s1, Segment s2){
    if(isIntersectedLs(s1.p1, s1.p2, s2.p1, s2.p2) ) return 0.0;
    return min(
            min(distanceLsPoint(s1.p1, s1.p2, s2.p1),
                distanceLsPoint(s1.p1, s1.p2, s2.p2)),
            min(distanceLsPoint(s2.p1, s2.p2, s1.p1),
                distanceLsPoint(s2.p1, s2.p2, s1.p2)) );
}

// 線分sに対する点pの射影
Point project(Segment s, Point p){
    Vector base = s.p2 - s.p1;
    double r = dot(p - s.p1, base) / norm(base);
    return Point(s.p1 + base * r);
}

//線分sを対象軸とした点pの線対称の点
Point reflect(Segment s, Point p){
    return Point(p + (project(s, p) - p) * 2.0);
}

//点pをangle分だけ時計回りに回転
Point rotation(Point p, double angle){
    double x, y;
    x = p.real() * cos(angle) - p.imag() * sin(angle);
    y = p.real() * sin(angle) + p.imag() * cos(angle);
    return Point(x, y);
}

//円cと線分lの交点
pair<Point, Point> getCrossPoints(Circle c, Line l){
    Vector pr = project(l, c.c);
    Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
    double base = sqrt(c.r * c.r - norm(pr - c.c));
    return make_pair(pr + e * base, pr - e * base);
}

//円c1と円c2の交点
double arg(Vector p) { return atan2(p.imag(), p.real()); }
Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) *a); }

pair<Point, Point> getCrossPoints(Circle c1, Circle c2){
    double d = abs(c1.c - c2.c);
    double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
    double t = arg(c2.c - c1.c);
    return make_pair(Point(c1.c + polar(c1.r, t + a)), Point(c1.c + polar(c1.r, t - a)));
}

//点の内包
static const int IN = 2;
static const int ON = 1;
static const int OUT = 0;

int contains(Polygon g, Point p){
    int n = g.size();
    bool x = false;
    rep(i,n){
        Point a = g[i] - p, b = g[(i + 1) % n] - p;
        if( abs(cross(a, b)) < EPS && dot(a,  b) < EPS ) return ON;
        if( a.imag() > b.imag() ) swap(a, b);
        if( a.imag() < EPS && EPS < b.imag() && cross(a, b) > EPS ) x = not x;
    }
    return ( x ? IN : OUT );
}

//ベクトルの位置検出
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

int ccw(Point p0, Point p1, Point p2){
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if( cross(a, b) > EPS ) return COUNTER_CLOCKWISE;
    if( cross(a, b) < -EPS ) return CLOCKWISE;
    if( dot(a, b) < -EPS ) return ONLINE_BACK;
    if( abs(a) < abs(b) ) return ONLINE_FRONT;

    return ON_SEGMENT;
}

//凸包
Polygon andrewScan( Polygon s ){
    Polygon u, l;
    if( s.size() < 3 ) return s;
    sort(s.begin(), s.end());

    u.emplace_back(s[0]);
    u.emplace_back(s[1]);

    l.emplace_back(s[s.size() - 1]);
    l.emplace_back(s[s.size() - 2]);

    range(i,2,s.size()){
        //== COUNTER_CLOCKWISEだと内角は180以下（一直線上に並んでいても、頂点として数える）
        //!= CLOCKWISEだと内角は180未満（一直線上の頂点は数えない）
        for(int n = u.size(); n >= 2 && ccw(u[n-2], u[n-1], s[i]) != CLOCKWISE; n--){
            u.pop_back();
        }
        u.emplace_back(s[i]);
    }

    for(int i = s.size() - 3; i >= 0; i--){
        for(int n = l.size(); n >= 2 && ccw(l[n-2], l[n-1], s[i]) != CLOCKWISE; n--){
            l.pop_back();
        }
        l.emplace_back(s[i]);
    }

    reverse(l.begin(), l.end());
    for(int i = u.size() - 2; i >= 1; i--) l.emplace_back(u[i]);

    return l;
}

class Node{
    public:
        double dis;
        bool isUsed;
        Node(){
            this->dis = INF;
            this->isUsed = 0;
        }
};

vector<Circle> cir;

double dijkstra(int n, double a[1005][1005]){
    vector<Node> node(n);
    priority_queue<int, vector<pair<double, int>>, greater<pair<double, int>>> q;

    q.push(make_pair(0,0));
    node[0].dis = 0;

    pair<double, int> u;
    while(not q.empty()){
        u = q.top(); q.pop();
        int current = u.second;
        node[current].isUsed = 1;

        rep(i,n){
            if(node[i].isUsed == 0){
                if(node[i].dis > node[current].dis + a[current][i]){
                    node[i].dis = node[current].dis + a[current][i];
                    q.push(make_pair(node[i].dis, i));
                }
            }
        }
    }
    return node[1].dis;
}

int main(){
    int n;

    double sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    Point s = Point{sx, sy};
    Point t = Point{tx, ty};
    cir.emplace_back(s, 0);
    cir.emplace_back(t, 0);

    cin >> n;
    rep(i,n){
        double a, b, c;
        cin >> a >> b >> c;
        cir.emplace_back(Circle{Point{a, b}, c});
    }

    double a[1005][1005];
    rep(i,cir.size()){
        rep(j,cir.size()){
            a[i][j] = max(0.0, abs(cir[i].c - cir[j].c) - (cir[i].r + cir[j].r));
            //show(a[i][j])
        }
    }
    cout <<fixed << setprecision(12) << dijkstra(n + 2, a) << endl;
}