Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin >>
#define Z class
#define ll long long
#define ln cout << '\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
template <Z A> void pr(A a) {
  cout << a;
  ln;
}
template <Z A, Z B> void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
const ll MAX = 1000000007, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
         dy[4] = {0, 1, 0, -1};
const double INF = 1e12;
const double EPS = 1e-10;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
bool cmp(P a, P b) {
  return atan2(a.imag(), a.real()) < atan2(b.imag(), b.real());
}
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }
struct C {
  P p;
  double r;
};
double D(P a, P b) {
  return sqrt((a.real() - b.real()) * (a.real() - b.real()) +
              (a.imag() - b.imag()) * (a.imag() - b.imag()));
}

void Main() {
  vector<C> a(1111);
  rep(i, 2) {
    double x, y;
    cin >> x >> y;
    a[i] = (C){P(x, y), 0};
  }
  int n;
  R n;
  rep(i, n) {
    double x, y, z;
    cin >> x >> y >> z;
    a[i + 2] = (C){P(x, y), z};
  }
  n += 2;
  double d[n];
  fill(d, d + n + 2, INF);
  vector<P> v[n];
  rep(i, n) {
    REP(j, i + 1, n) {
      double d = D(a[i].p, a[j].p);
      double t = a[i].r + a[j].r;
      d = max(0.0, d - t);
      v[i].pb(P(j, d));
      v[j].pb(P(i, d));
    }
  }
  d[0] = 0;
  priority_queue<P> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int x = p.imag();
    double cc = p.real();
    if (d[x] < cc)
      continue;
    rep(i, v[x].size()) {
      P q = v[x][i];
      int y = q.real();
      double c = q.imag();
      if (d[y] > d[x] + c) {
        d[y] = d[x] + c;
        que.push(P(d[y], y));
      }
    }
  }
  pd(d[1]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin >>
#define Z class
#define ll long long
#define ln cout << '\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
template <Z A> void pr(A a) {
  cout << a;
  ln;
}
template <Z A, Z B> void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
const ll MAX = 1000000007, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
         dy[4] = {0, 1, 0, -1};
const double INF = 1e12;
const double EPS = 1e-10;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) > real(b) : imag(a) > imag(b);
}
} // namespace std
bool cmp(P a, P b) {
  return atan2(a.imag(), a.real()) < atan2(b.imag(), b.real());
}
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }
struct C {
  P p;
  double r;
};
double D(P a, P b) {
  return sqrt((a.real() - b.real()) * (a.real() - b.real()) +
              (a.imag() - b.imag()) * (a.imag() - b.imag()));
}

void Main() {
  vector<C> a(1111);
  rep(i, 2) {
    double x, y;
    cin >> x >> y;
    a[i] = (C){P(x, y), 0};
  }
  int n;
  R n;
  rep(i, n) {
    double x, y, z;
    cin >> x >> y >> z;
    a[i + 2] = (C){P(x, y), z};
  }
  n += 2;
  double d[n];
  fill(d, d + n + 2, INF);
  vector<P> v[n];
  rep(i, n) {
    REP(j, i + 1, n) {
      double d = D(a[i].p, a[j].p);
      double t = a[i].r + a[j].r;
      d = max(0.0, d - t);
      v[i].pb(P(j, d));
      v[j].pb(P(i, d));
    }
  }
  d[0] = 0;
  priority_queue<P> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int x = p.imag();
    double cc = p.real();
    if (d[x] < cc)
      continue;
    rep(i, v[x].size()) {
      P q = v[x][i];
      int y = q.real();
      double c = q.imag();
      if (d[y] > d[x] + c) {
        d[y] = d[x] + c;
        que.push(P(d[y], y));
      }
    }
  }
  pd(d[1]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}