Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f1(a, b, c) for (int c = a; c <= b; c++)
#define f2(a, b, c) for (int c = a; c >= b; c--)
#define f3(a, b, c) for (int c = a; c; c = b)
#define so1(a, n) sort(a + 1, a + n + 1, mycmp);
#define so2(a, n) sort(a + 1, a + n + 1);
#define ll long long
#define itn int
#define ubt int
const int twx = 20000 + 100;
const int inf = 0x3f3f3f3f;
ll read() {
  ll sum = 0;
  ll flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      flag = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = ((sum * 10) + c - '0');
    c = getchar();
  }
  return sum * flag;
}
int n, m, l;
struct LV {
  int y;
  int Next;
  int v;
} a[twx << 1];
int Link[twx];
int len;
int d[twx];
int v[twx];
int f[301][301];
priority_queue<pair<int, int>> q;
void dijkstra(int s) {
  memset(v, 0, sizeof v);
  memset(d, 0x3f, sizeof d);
  q.push(make_pair(0, s));
  d[s] = 0;
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    if (v[x]) {
      continue;
    }
    v[x] = 1;
    f3(Link[x], a[i].Next, i) {
      int y = a[i].y;
      int v = a[i].v;
      if (d[y] > d[x] + v && d[x] + v <= l) {
        d[y] = d[x] + v;
        f[s][y] = 0;
        q.push(make_pair(-d[y], y));
      }
    }
  }
}
void floyed() {
  f1(1, n, k) {
    f1(1, n, i) {
      f1(1, n, j) { f[i][j] = min(f[i][j], f[i][k] + f[k][j] + 1); }
    }
  }
}
void Insert(int x, int y, int z) {
  a[++len].y = y;
  a[len].v = z;
  a[len].Next = Link[x];
  Link[x] = len;
}
void init() {
  n = read();
  m = read();
  l = read();
  f1(1, m, i) {
    int x = read();
    int y = read();
    int z = read();
    Insert(x, y, z);
    Insert(y, x, z);
  }
  memset(f, 0x3f, sizeof f);
  f1(1, n, i) { dijkstra(i); }
  int qq = read();
  floyed();
  while (qq--) {
    int x = read();
    int y = read();
    printf("%d\n", f[x][y] == inf ? -1 : f[x][y]);
  }
}
void work() {}
void print() {}
int main() {
  init();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f1(a, b, c) for (int c = a; c <= b; c++)
#define f2(a, b, c) for (int c = a; c >= b; c--)
#define f3(a, b, c) for (int c = a; c; c = b)
#define so1(a, n) sort(a + 1, a + n + 1, mycmp);
#define so2(a, n) sort(a + 1, a + n + 1);
#define ll long long
#define itn int
#define ubt int
const int twx = 100000 + 100;
const int inf = 0x3f3f3f3f;
ll read() {
  ll sum = 0;
  ll flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      flag = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = ((sum * 10) + c - '0');
    c = getchar();
  }
  return sum * flag;
}
int n, m, l;
struct LV {
  int y;
  int Next;
  int v;
} a[twx << 1];
int Link[twx];
int len;
int d[twx];
int v[twx];
int f[301][301];
priority_queue<pair<int, int>> q;
void dijkstra(int s) {
  memset(v, 0, sizeof v);
  memset(d, 0x3f, sizeof d);
  q.push(make_pair(0, s));
  d[s] = 0;
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    if (v[x]) {
      continue;
    }
    v[x] = 1;
    f3(Link[x], a[i].Next, i) {
      int y = a[i].y;
      int v = a[i].v;
      if (d[y] > d[x] + v && d[x] + v <= l) {
        d[y] = d[x] + v;
        f[s][y] = 0;
        q.push(make_pair(-d[y], y));
      }
    }
  }
}
void floyed() {
  f1(1, n, k) {
    f1(1, n, i) {
      f1(1, n, j) { f[i][j] = min(f[i][j], f[i][k] + f[k][j] + 1); }
    }
  }
}
void Insert(int x, int y, int z) {
  a[++len].y = y;
  a[len].v = z;
  a[len].Next = Link[x];
  Link[x] = len;
}
void init() {
  n = read();
  m = read();
  l = read();
  f1(1, m, i) {
    int x = read();
    int y = read();
    int z = read();
    Insert(x, y, z);
    Insert(y, x, z);
  }
  memset(f, 0x3f, sizeof f);
  f1(1, n, i) { dijkstra(i); }
  int qq = read();
  floyed();
  while (qq--) {
    int x = read();
    int y = read();
    printf("%d\n", f[x][y] == inf ? -1 : f[x][y]);
  }
}
void work() {}
void print() {}
int main() {
  init();
  return 0;
}