#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
/*----------------------------------ここからマクロ----------------------------------*/
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(n) for(int i=0;i<(int)n;++i)
#define rep2(i,n) for(int i=0;i<(int)n;++i)
#define rep3(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define rep4(i,a,b,c) for(int i=(int)a;i<(int)b;i+=(int)c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#ifdef _DEBUG
#define debug1(a) cerr << #a << ": " << a << "\n"
#define debug2(a,b) cerr << #a << ": " << a << ", " << #b << ": " << b << "\n"
#define debug3(a,b,c) cerr << #a << ": " << a << ", " << #b << ": " << b << ", " << #c << ": " << c << "\n"
#define debug4(a,b,c,d) cerr << #a << ": " << a << ", " << #b << ": " << b << ", " << #c << ": " << c << ", " << #d << ": " << d << "\n"
#define debug(...) overload4(__VA_ARGS__,debug4,debug3,debug2,debug1)(__VA_ARGS__)
#define vecout(a) cerr << #a << ": [";rep(i,a.size()){cout << a[i];cout << (i == a.size() - 1 ? "":",");}cerr << "]\n"
#else
#define debug(...)
#define vecout(a)
#endif
#define mp make_pair
//struct doset{doset(int n){cout << fixed << setprecision(n);cerr << fixed << setprecision(n);}};
//struct myset{myset(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}};
void myset(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
void doset(int n){cout << fixed << setprecision(n);}
using ll = long long;
using ld = long double;
using dou = double;
const int inf = 1 << 30;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod1 = 1000000007LL;
const ll mod2 = 998244353LL;
typedef pair<ll,ll> P;
template<class T, class U> inline bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> inline bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> inline bool change(T& a,U& b){if(a > b){swap(a,b);return 1;}return 0;}
//nのm乗をMODで割ったあまりO(logm)
ll modpow(ll n,ll m,ll MOD){
    if(m == 0)return 1;
    if(m < 0)return -1;
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

ll mypow(ll n,ll m){
    if(m == 0)return 1;
    if(m < 0)return -1;
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n);
        m >>= 1;
        n *= n;
    }
    return res;
}

//素数判定O(sqrt(N))
template<class T>
inline bool isp(T n){
    bool res = true;
    if(n == 1 || n == 0)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

template<class T = int>
T in(){T x;cin >> x;return x;}

inline bool Yes(bool b){cout << (b ? "Yes\n":"No\n");return b;}
inline bool YES(bool b){cout << (b ? "YES\n":"NO\n");return b;}

/*----------------------------------マクロここまで----------------------------------*/

ll __lcm(ll a,ll b){
    return a / __gcd(a,b) * b;
}

ld circle_dist(ld x1,ld y1,ld r1,ld x2,ld y2,ld r2){
    //接しているか被っているかdistが0か
    ld rs = r1 + r2,cen = abs(x1 - x2) * abs(x1 - x2) + abs(y1 - y2) * abs(y1 - y2);
    rs *= rs;
    if(rs >= cen)return 0.0;
    ld sqrs = r1 + r2,sqcen = sqrt(abs(x1 - x2) * abs(x1 - x2) + abs(y1 - y2) * abs(y1 - y2));
    return sqcen - sqrs;
}

typedef pair<ld,int> pii;
int main(){
    myset();
    ld sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    int N;
    cin >> N;
    vector<ld> X(N + 2),Y(N + 2),R(N + 2);
    X[0] = sx;Y[0] = sy;R[0] = 0.0;
    X[N + 1] = gx;Y[N + 1] = gy;R[N + 1] = 0.0;
    rep(i,1,N + 1){
        cin >> X[i] >> Y[i] >> R[i];
    }
    struct edge{int to;ld cost;};
    vector<vector<edge>> G(N + 2);
    rep(i,N + 2){
        rep(j,i + 1,N + 2){
            ld d = circle_dist(X[i],Y[i],R[i],X[j],Y[j],R[j]);
            G[i].push_back({j,d});
            G[j].push_back({i,d});
        }
    }
    priority_queue<pii,vector<pii>,greater<pii>> que;
    vector<ld> dist(N + 2,4000000000.0);
    dist[0] = 0.0;
    que.push({0.0,0});
    while(!que.empty()){
        pii p = que.top();
        que.pop();
        if(p.first > dist[p.second])continue;
        for(auto x : G[p.second]){
            if(chmin(dist[x.to],dist[p.second] + x.cost)){
                que.push({dist[x.to],x.to});
            }
        }
    }
    doset(10);
    vecout(dist);
    cout << dist[N + 1] << "\n";
}

Measured memory_bytes: 44224