Problem p03866 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define re return
#define mp make_pair
#define makeunique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define endl '\n'

typedef pair<int, int> ii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<string> vs;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T sqr(T x) { re x *x; }

const double pi = acos(-1.);
const int inf = 1e9 + 7;
const int N = 3e5 + 17;

int n, m;
double g[1010][1010];
double x[1010], y[1010], r[1010];
double dist[1010];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> r[i];
  }
  x[n] = x1, y[n] = y1;
  x[n + 1] = x2, y[n + 1] = y2;
  n += 2;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      double d = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]));
      d = max(double(0), d - r[i] - r[j]);
      g[i][j] = d;
    }
  for (int i = 0; i < n; i++)
    dist[i] = inf;
  dist[n - 2] = 0;
  vi was(n);
  for (int z = 0; z < n; z++) {
    int best = -1;
    double cur = inf;
    for (int i = 0; i < n; i++)
      if (cur > dist[i] && !was[i]) {
        cur = dist[i];
        best = i;
      }
    was[best] = 1;
    for (int i = 0; i < n; i++)
      if (!was[i] && dist[i] > cur + g[best][i]) {
        dist[i] = cur + g[best][i];
      }
  }
  cout << setprecision(30) << dist[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define re return
#define mp make_pair
#define makeunique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define endl '\n'

typedef pair<int, int> ii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<string> vs;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T sqr(T x) { re x *x; }

const double pi = acos(-1.);
const ll inf = 1e18 + 7;
const int N = 3e5 + 17;

int n, m;
double g[1010][1010];
double x[1010], y[1010], r[1010];
double dist[1010];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> r[i];
  }
  x[n] = x1, y[n] = y1;
  x[n + 1] = x2, y[n + 1] = y2;
  n += 2;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      double d = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]));
      d = max(double(0), d - r[i] - r[j]);
      g[i][j] = d;
    }
  for (int i = 0; i < n; i++)
    dist[i] = inf;
  dist[n - 2] = 0;
  vi was(n);
  for (int z = 0; z < n; z++) {
    int best = -1;
    double cur = inf;
    for (int i = 0; i < n; i++)
      if (cur > dist[i] && !was[i]) {
        cur = dist[i];
        best = i;
      }
    was[best] = 1;
    for (int i = 0; i < n; i++)
      if (!was[i] && dist[i] > cur + g[best][i]) {
        dist[i] = cur + g[best][i];
      }
  }
  cout << setprecision(30) << dist[n - 1];
}