Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
#define maxn 305
inline ll read() {
  ll x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return w ? -x : x;
}
int n, m, l, Q;
int headd[maxn], nextt[maxn << 1], to[maxn << 1], val[maxn << 1], cntt;
int dp[maxn][maxn], ss[maxn], vis[maxn];
inline void add(int u, int v, int w) {
  nextt[++cntt] = headd[u];
  headd[u] = cntt;
  to[cntt] = v, val[cntt] = w;
}
struct node {
  int x, y, z;
  bool operator<(const node &b) const {
    if (y == b.y)
      return z < b.z;
    return y > b.y;
  }
};
priority_queue<node> q;
inline void dij(int x) {
  memset(vis, 0, sizeof vis);
  memset(dp[x], 63, sizeof dp[x]);
  memset(ss, 63, sizeof ss);
  dp[x][x] = 0;
  ss[x] = l;
  node u;
  reg int i, k, o;
  q.push((node){x, 0, l});
  while (!q.empty()) {
    u = q.top();
    q.pop();
    if (vis[u.x])
      continue;
    vis[u.x] = 1;
    for (i = headd[u.x]; i; i = nextt[i]) {
      k = -1;
      if (u.z < val[i] && l >= val[i])
        k = 1, o = l - val[i];
      if (u.z >= val[i])
        k = 0, o = u.z - val[i];
      if (k == -1)
        continue;
      if (dp[x][to[i]] > dp[x][u.x] + k) {
        dp[x][to[i]] = dp[x][u.x] + k;
        ss[to[i]] = o;
        q.push((node){to[i], dp[x][to[i]], ss[to[i]]});
      } else {
        if (dp[x][to[i]] == dp[x][u.x] + k && ss[to[i]] < o)
          ss[to[i]] = o, q.push((node){to[i], dp[x][to[i]], ss[to[i]]});
      }
    }
  }
}
int main() {
  n = read(), m = read(), l = read();
  reg int i, u, v, w;
  for (i = 1; i <= m; ++i) {
    u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  for (i = 1; i <= n; ++i)
    dij(i);
  Q = read();
  while (Q--) {
    u = read(), v = read();
    if (dp[u][v] > n)
      puts("-1");
    else
      printf("%d\n", dp[u][v]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
#define maxn 305
inline ll read() {
  ll x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return w ? -x : x;
}
int n, m, l, Q;
int headd[maxn * maxn], nextt[maxn * maxn], to[maxn * maxn], val[maxn * maxn],
    cntt;
int dp[maxn][maxn], ss[maxn], vis[maxn];
inline void add(int u, int v, int w) {
  nextt[++cntt] = headd[u];
  headd[u] = cntt;
  to[cntt] = v, val[cntt] = w;
}
struct node {
  int x, y, z;
  bool operator<(const node &b) const {
    if (y == b.y)
      return z < b.z;
    return y > b.y;
  }
};
priority_queue<node> q;
inline void dij(int x) {
  memset(vis, 0, sizeof vis);
  memset(dp[x], 63, sizeof dp[x]);
  memset(ss, 63, sizeof ss);
  dp[x][x] = 0;
  ss[x] = l;
  node u;
  reg int i, k, o;
  q.push((node){x, 0, l});
  while (!q.empty()) {
    u = q.top();
    q.pop();
    if (vis[u.x])
      continue;
    vis[u.x] = 1;
    for (i = headd[u.x]; i; i = nextt[i]) {
      k = -1;
      if (u.z < val[i] && l >= val[i])
        k = 1, o = l - val[i];
      if (u.z >= val[i])
        k = 0, o = u.z - val[i];
      if (k == -1)
        continue;
      if (dp[x][to[i]] > dp[x][u.x] + k) {
        dp[x][to[i]] = dp[x][u.x] + k;
        ss[to[i]] = o;
        q.push((node){to[i], dp[x][to[i]], ss[to[i]]});
      } else {
        if (dp[x][to[i]] == dp[x][u.x] + k && ss[to[i]] < o)
          ss[to[i]] = o, q.push((node){to[i], dp[x][to[i]], ss[to[i]]});
      }
    }
  }
}
int main() {
  n = read(), m = read(), l = read();
  reg int i, u, v, w;
  for (i = 1; i <= m; ++i) {
    u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  for (i = 1; i <= n; ++i)
    dij(i);
  Q = read();
  while (Q--) {
    u = read(), v = read();
    if (dp[u][v] > n)
      puts("-1");
    else
      printf("%d\n", dp[u][v]);
  }
  return 0;
}