Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll inf = 1e17;
ll N, M, L, Q;
ll fuel[305][305];
ll dist[305][305];
ll s[10500], t[10500];
ll A[100000], B[100000], C[100000];

void input() {
  cin >> N >> M >> L;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i] >> C[i];
  cin >> Q;
  for (int i = 1; i <= Q; i++)
    cin >> s[i] >> t[i];
}

int main() {
  input();
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      fuel[i][j] = inf;
      dist[i][j] = inf;
    }
    fuel[i][i] = 0;
    dist[i][i] = 0;
  }
  for (int i = 1; i <= M; i++) {
    chmin(fuel[A[i]][B[i]], C[i]);
    chmin(fuel[B[i]][A[i]], C[i]);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        chmin(fuel[j][k], fuel[j][i] + fuel[i][k]);
      }
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i == j)
        continue;
      if (fuel[i][j] <= L)
        chmin(dist[i][j], 1ll);
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        chmin(dist[j][k], dist[j][i] + dist[i][k]);
      }
    }
  }
  for (int i = 1; i <= Q; i++) {
    if (dist[t[i]][s[i]] < inf)
      cout << dist[t[i]][s[i]] - 1 << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll inf = 1e17;
ll N, M, L, Q;
ll fuel[305][305];
ll dist[305][305];
ll s[105000], t[105000];
ll A[100000], B[100000], C[100000];

void input() {
  cin >> N >> M >> L;
  for (int i = 1; i <= M; i++)
    cin >> A[i] >> B[i] >> C[i];
  cin >> Q;
  for (int i = 1; i <= Q; i++)
    cin >> s[i] >> t[i];
}

int main() {
  input();
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      fuel[i][j] = inf;
      dist[i][j] = inf;
    }
    fuel[i][i] = 0;
    dist[i][i] = 0;
  }
  for (int i = 1; i <= M; i++) {
    chmin(fuel[A[i]][B[i]], C[i]);
    chmin(fuel[B[i]][A[i]], C[i]);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        chmin(fuel[j][k], fuel[j][i] + fuel[i][k]);
      }
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i == j)
        continue;
      if (fuel[i][j] <= L)
        chmin(dist[i][j], 1ll);
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        chmin(dist[j][k], dist[j][i] + dist[i][k]);
      }
    }
  }
  for (int i = 1; i <= Q; i++) {
    if (dist[t[i]][s[i]] < inf)
      cout << dist[t[i]][s[i]] - 1 << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}