Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A FAN OF FIZZYDAVID

*/

#include <bits/stdc++.h>

#define HEAP priority_queue
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define forn(i, l, r) for (int i = (l); i <= (r); i++)
#define nrof(i, r, l) for (int i = (r); i >= (l); i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define X first
#define Y second
#define eps 1e-9
#define pi 3.1415926535897932384626433832795
#define orz int
#define yjz main
#define fizzydavid return
#define ak 0
#define la ;
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(), x.end()
#define FILL(a, b) memset((a), (b), sizeof((a)))

using namespace std;

typedef long long LL;
typedef double flt;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<LL> vl;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
typedef pair<LL, LL> pll;
typedef vector<pil> vil;
typedef vector<pii> vii;

const int iinf = 1e9 + 7;
const LL linf = 1ll << 60;
const flt dinf = 1e10;

template <typename T> inline void scf(T &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f)
    x = -x;
  return;
}

template <typename T1, typename T2> void scf(T1 &x, T2 &y) {
  scf(x);
  return scf(y);
}

template <typename T1, typename T2, typename T3> void scf(T1 &x, T2 &y, T3 &z) {
  scf(x);
  scf(y);
  return scf(z);
}

template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
  scf(x);
  scf(y);
  scf(z);
  return scf(w);
}

#ifdef ONLINE_JUDGE
#define debug(x) ;
#else
#define DEBUG
#define debug(x) cerr << #x << "=" << x << endl;
#endif

//---------------------------head----------------------------

const int N = 1510;

int n, S, T;
int xs, ys, xt, yt;
int x[N], y[N], r[N];
vector<pair<int, flt>> g[N];
int q[N * N];
flt dst[N];
bool in[N];

inline int ABS(int x) { return x < 0 ? -x : x; }

orz yjz() {
  S = 0;
  scf(xs, ys, xt, yt);
  scf(n);
  forn(i, 1, n) { scf(x[i], y[i], r[i]); }
  T = n + 1;

  forn(i, 1, n) forn(j, i + 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - x[j]), 1.0 * ABS(y[i] - y[j]));
    d -= (1.0 * (r[i] + r[j]));

    if (d <= eps)
      d = 0.0;

    g[i].pb(mp(j, d));
    g[j].pb(mp(i, d));
  }

  forn(i, 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - xs), 1.0 * ABS(y[i] - ys));
    d -= (1.0 * r[i]);

    if (d <= eps)
      d = 0.0;

    g[S].pb(mp(i, d));
  }

  forn(i, 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - xt), 1.0 * ABS(y[i] - yt));
    d -= (1.0 * r[i]);

    if (d <= eps)
      d = 0.0;

    g[i].pb(mp(T, d));
  }

  forn(i, 1, T) dst[i] = 1e20;
  int fnt, rar;
  fnt = rar = 0;
  q[rar++] = S;
  while (fnt != rar) {
    int u = q[fnt++];
    in[u] = 0;

    for (auto e : g[u]) {
      int v = e.X;
      flt w = e.Y;
      if (w + dst[u] <= dst[v] - eps) {
        dst[v] = dst[u] + w;
        if (!in[v]) {
          in[v] = 1;
          q[rar++] = v;
        }
      }
    }
  }

  printf("%.12f\n", min(dst[T], hypot(1.0 * (xt - xs), 1.0 * (yt - ys))));
  fizzydavid ak la
}


Fixed submission:
/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A FAN OF FIZZYDAVID

*/

#include <bits/stdc++.h>

#define HEAP priority_queue
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define forn(i, l, r) for (int i = (l); i <= (r); i++)
#define nrof(i, r, l) for (int i = (r); i >= (l); i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define X first
#define Y second
#define eps 1e-9
#define pi 3.1415926535897932384626433832795
#define orz int
#define yjz main
#define fizzydavid return
#define ak 0
#define la ;
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(), x.end()
#define FILL(a, b) memset((a), (b), sizeof((a)))

using namespace std;

typedef long long LL;
typedef double flt;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<LL> vl;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
typedef pair<LL, LL> pll;
typedef vector<pil> vil;
typedef vector<pii> vii;

const int iinf = 1e9 + 7;
const LL linf = 1ll << 60;
const flt dinf = 1e10;

template <typename T> inline void scf(T &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f)
    x = -x;
  return;
}

template <typename T1, typename T2> void scf(T1 &x, T2 &y) {
  scf(x);
  return scf(y);
}

template <typename T1, typename T2, typename T3> void scf(T1 &x, T2 &y, T3 &z) {
  scf(x);
  scf(y);
  return scf(z);
}

template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
  scf(x);
  scf(y);
  scf(z);
  return scf(w);
}

#ifdef ONLINE_JUDGE
#define debug(x) ;
#else
#define DEBUG
#define debug(x) cerr << #x << "=" << x << endl;
#endif

//---------------------------head----------------------------

const int N = 1510;

int n, S, T;
int xs, ys, xt, yt;
int x[N], y[N], r[N];
vector<pair<int, flt>> g[N];
int q[N * N];
flt dst[N];
bool in[N];

inline int ABS(int x) { return x < 0 ? -x : x; }

orz yjz() {
  S = 0;
  scf(xs, ys, xt, yt);
  scf(n);
  forn(i, 1, n) { scf(x[i], y[i], r[i]); }
  T = n + 1;

  forn(i, 1, n) forn(j, i + 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - x[j]), 1.0 * ABS(y[i] - y[j]));
    d -= (1.0 * (r[i] + r[j]));

    if (d <= eps)
      d = 0.0;

    g[i].pb(mp(j, d));
    g[j].pb(mp(i, d));
  }

  forn(i, 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - xs), 1.0 * ABS(y[i] - ys));
    d -= (1.0 * r[i]);

    if (d <= eps)
      d = 0.0;

    g[S].pb(mp(i, d));
  }

  forn(i, 1, n) {
    flt d = hypot(1.0 * ABS(x[i] - xt), 1.0 * ABS(y[i] - yt));
    d -= (1.0 * r[i]);

    if (d <= eps)
      d = 0.0;

    g[i].pb(mp(T, d));
  }

  forn(i, 1, T) dst[i] = 1e20;
  int fnt, rar;
  fnt = rar = 0;
  q[rar++] = S;
  while (fnt != rar) {
    int u = q[fnt++];
    in[u] = 0;

    for (auto e : g[u]) {
      int v = e.X;
      flt w = e.Y;
      if (w + dst[u] < dst[v] - eps) {
        dst[v] = dst[u] + w;
        if (!in[v]) {
          in[v] = 1;
          q[rar++] = v;
        }
      }
    }
  }

  printf("%.12f\n", min(dst[T], hypot(1.0 * (xt - xs), 1.0 * (yt - ys))));
  fizzydavid ak la
}