Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define db double
#define N 1005
using namespace std;
struct P {
  int x, y, r;
} p[N];
int tot = 0, px, py, pr, n, vis[N];
struct edge {
  int to, nxt;
  db w;
} e[N * N * 2];
int head[N], cnt = 0;
inline void add(register int u, register int v, register db w) {
  ++cnt;
  e[cnt].to = v, e[cnt].nxt = head[u], e[cnt].w = w;
  head[u] = cnt;
}
db dis[N];
struct node {
  int pos;
  db dis;
  bool operator<(const node &x) const { return x.dis < dis; }
};
priority_queue<node> q;
inline void dij() {
  for (register int i = 1; i <= tot; ++i)
    dis[i] = 114514191981.0;
  dis[1] = 0.0;
  node tmp;
  tmp.pos = 1, tmp.dis = 0.0;
  q.push(tmp);
  while (!q.empty()) {
    int u = q.top().pos;
    q.pop();
    for (register int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        tmp.pos = v, tmp.dis = dis[v];
        q.push(tmp);
      }
    }
  }
}
int main() {
  for (register int i = 1; i <= 2; ++i) {
    scanf("%d%d", &px, &py);
    p[++tot] = (P){px, py, 0};
  }
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &px, &py, &pr);
    p[++tot] = (P){px, py, pr};
  }
  for (register int i = 1; i < tot; ++i)
    for (register int j = i + 1; j <= tot; ++j) {
      db w = sqrt(1.0 * (p[i].x - p[j].x) * (p[i].x - p[j].x) +
                  1.0 * (p[i].y - p[j].y) * (p[i].y - p[j].y)) -
             1.0 * p[i].r - 1.0 * p[j].r;
      if (w < 0)
        w = 0.0;
      add(i, j, w), add(j, i, w);
    }
  dij();
  printf("%.10lf\n", dis[2]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define db double
#define N 1005
using namespace std;
struct P {
  int x, y, r;
} p[N];
int tot = 0, px, py, pr, n, vis[N];
struct edge {
  int to, nxt;
  db w;
} e[N * N * 2];
int head[N], cnt = 0;
inline void add(register int u, register int v, register db w) {
  ++cnt;
  e[cnt].to = v, e[cnt].nxt = head[u], e[cnt].w = w;
  head[u] = cnt;
}
db dis[N];
struct node {
  int pos;
  db dis;
  bool operator<(const node &x) const { return x.dis < dis; }
};
priority_queue<node> q;
inline void dij() {
  for (register int i = 1; i <= tot; ++i)
    dis[i] = 114514191981.0;
  dis[1] = 0.0;
  node tmp;
  tmp.pos = 1, tmp.dis = 0.0;
  q.push(tmp);
  while (!q.empty()) {
    int u = q.top().pos;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (register int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        tmp.pos = v, tmp.dis = dis[v];
        q.push(tmp);
      }
    }
  }
}
int main() {
  for (register int i = 1; i <= 2; ++i) {
    scanf("%d%d", &px, &py);
    p[++tot] = (P){px, py, 0};
  }
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &px, &py, &pr);
    p[++tot] = (P){px, py, pr};
  }
  for (register int i = 1; i < tot; ++i)
    for (register int j = i + 1; j <= tot; ++j) {
      db w = sqrt(1.0 * (p[i].x - p[j].x) * (p[i].x - p[j].x) +
                  1.0 * (p[i].y - p[j].y) * (p[i].y - p[j].y)) -
             1.0 * p[i].r - 1.0 * p[j].r;
      if (w < 0)
        w = 0.0;
      add(i, j, w), add(j, i, w);
    }
  dij();
  printf("%.10lf\n", dis[2]);
  return 0;
}