Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// ll MOD=1000000007;
ll INF = 300000000000;
int main() {
  ll N, M, L;
  cin >> N >> M >> L;
  vector<ll> A(N), B(N), C(N);
  for (ll i = 0; i < M; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    A[i] = a;
    B[i] = b;
    C[i] = c;
  }
  ll Dist[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      Dist[i][j] = INF;
    }
  }
  for (int i = 0; i < N; i++) {
    Dist[i][i] = 0;
  }
  for (int i = 0; i < M; i++) {
    Dist[A[i]][B[i]] = C[i];
    Dist[B[i]][A[i]] = C[i];
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]);
      }
    }
  }
  ll Way[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      Way[i][j] = 301;
    }
  }
  for (int i = 0; i < N; i++) {
    Way[i][i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i != j) {
        if (Dist[i][j] <= L) {
          Way[i][j] = 1;
        }
      }
    }
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        Way[i][j] = min(Way[i][j], Way[i][k] + Way[k][j]);
      }
    }
  }
  int Q;
  cin >> Q;
  vector<ll> S(Q), T(Q);
  for (int i = 0; i < Q; i++) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--; // ここを変えた
    S[i] = s;
    T[i] = t;
    if (Way[S[i]][T[i]] == 301) {
      cout << -1 << endl;
    } else {
      cout << Way[S[i]][T[i]] - 1 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// ll MOD=1000000007;
ll INF = 300000000000;
int main() {
  ll N, M, L;
  cin >> N >> M >> L;
  vector<ll> A(M), B(M), C(M);
  for (ll i = 0; i < M; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    A[i] = a;
    B[i] = b;
    C[i] = c;
  }
  ll Dist[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      Dist[i][j] = INF;
    }
  }
  for (int i = 0; i < N; i++) {
    Dist[i][i] = 0;
  }
  for (int i = 0; i < M; i++) {
    Dist[A[i]][B[i]] = C[i];
    Dist[B[i]][A[i]] = C[i];
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]);
      }
    }
  }
  ll Way[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      Way[i][j] = 301;
    }
  }
  for (int i = 0; i < N; i++) {
    Way[i][i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i != j) {
        if (Dist[i][j] <= L) {
          Way[i][j] = 1;
        }
      }
    }
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        Way[i][j] = min(Way[i][j], Way[i][k] + Way[k][j]);
      }
    }
  }
  int Q;
  cin >> Q;
  vector<ll> S(Q), T(Q);
  for (int i = 0; i < Q; i++) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--; // ここを変えた
    S[i] = s;
    T[i] = t;
    if (Way[S[i]][T[i]] == 301) {
      cout << -1 << endl;
    } else {
      cout << Way[S[i]][T[i]] - 1 << endl;
    }
  }
  return 0;
}