Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> Pii;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1e17
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define fs first
#define ss second
inline int Scan() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}

// call by reference is used in x
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
//////////////////////////////////////end of
///template//////////////////////////////////////

ll a[100000], b[100000], c[100000];

ll mat[305][305];
ll dist[305][305];

ll s[105000], t[105000];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m, l;
  cin >> n >> m >> l;

  for (int i = 1; i <= m; i++)
    cin >> a[i] >> b[i] >> c[i];

  ll q;
  cin >> q;

  for (int i = 1; i <= q; i++)
    cin >> s[i] >> t[i];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mat[i][j] = INF;
      dist[i][j] = INF;
    }
    mat[i][i] = 0;
    dist[i][i] = 0;
  }

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      amin(mat[a[i]][b[i]], c[i]);
      amin(mat[b[i]][a[i]], c[i]);
    }
  }

  // cout <<"INPUT"<<endl;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        amin(mat[j][k], mat[j][i] + mat[i][k]);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (mat[i][j] <= l)
        amin(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++) {
        amin(dist[j][k], dist[j][i] + dist[i][k]);
      }
    }
  }

  for (int i = 1; i <= q; i++) {
    if (dist[s[i]][t[i]] < INF)
      cout << dist[s[i]][t[i]] - 1 << endl;
    else
      cout << -1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> Pii;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1e17
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define fs first
#define ss second
inline int Scan() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}

// call by reference is used in x
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
//////////////////////////////////////end of
///template//////////////////////////////////////

ll a[100000], b[100000], c[100000];

ll mat[305][305];
ll dist[305][305];

ll s[105000], t[105000];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m, l;
  cin >> n >> m >> l;

  for (int i = 1; i <= m; i++)
    cin >> a[i] >> b[i] >> c[i];

  ll q;
  cin >> q;

  for (int i = 1; i <= q; i++)
    cin >> s[i] >> t[i];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mat[i][j] = INF;
      dist[i][j] = INF;
    }
    mat[i][i] = 0;
    dist[i][i] = 0;
  }

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      mat[a[i]][b[i]] = c[i];
      mat[b[i]][a[i]] = c[i];
    }
  }

  // cout <<"INPUT"<<endl;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        amin(mat[j][k], mat[j][i] + mat[i][k]);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (mat[i][j] <= l)
        amin(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++) {
        amin(dist[j][k], dist[j][i] + dist[i][k]);
      }
    }
  }

  for (int i = 1; i <= q; i++) {
    if (dist[s[i]][t[i]] < INF)
      cout << dist[s[i]][t[i]] - 1 << endl;
    else
      cout << -1 << endl;
  }
}