Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

// template {{{  0
// using {{{ 1
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n")
#define YESNO(a) (a) ? printf("YES\n") : printf("NO\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

struct edge {
  int to, dis;
};

int main() {

  int N, M, L;
  Scd3(N, M, L);

  vector<vector<edge>> e(N);
  vector<vll> t(N, vll(N, {INF, INF}));
  // t[kyuyu][used]
  Rep(i, N) t[i][i] = {0, 0};

  int a, b, c;
  Rep(i, M) {
    Scd3(a, b, c);
    a--, b--;
    if (c > L)
      continue;
    e[a].push_back({b, c});
    e[b].push_back({a, c});
  }

  priority_queue<pair<pll, int>> q;
  Rep(i, N) {
    q.push({{0, 0}, i});
    while (q.size()) {
      int from = q.top().second;
      pll dist = q.top().first;
      q.pop();
      if (t[i][from] < dist)
        continue;
      for (edge tt : e[from]) {
        int to = tt.to;
        int di = tt.dis;
        pll old = t[i][to];
        pll ne;
        if (dist.second + di > L) {
          ne = {dist.first + 1, di};
        } else {
          ne = {dist.first, dist.second + di};
        }
        if (ne < old) {
          t[i][to] = ne;
          q.push({ne, to});
        }
      }
    }
  }

  // Rep(i,N) Rep(j,N){
  //     printf ("%lld,%lld%c", t[i][j].first, t[i][j].second, j == N-1 ? '\n' :
  //     ' ' );
  // }

  int Q;
  Scd(Q);

  Rep(i, Q) {
    Scd2(a, b);
    a--, b--;
    printf("%lld\n", t[a][b].first == INF ? -1 : t[a][b].first);
  }

  // Rep(k,N) Rep(i,N) Rep(j,N){
  //     d[i][j] = min( d[i][j] , d[i][k] + d[k][j] );
  // }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

// template {{{  0
// using {{{ 1
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n")
#define YESNO(a) (a) ? printf("YES\n") : printf("NO\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

struct edge {
  int to, dis;
};

int main() {

  int N, M, L;
  Scd3(N, M, L);

  vector<vector<edge>> e(N);
  vector<vll> t(N, vll(N, {INF, INF}));
  // t[kyuyu][used]
  Rep(i, N) t[i][i] = {0, 0};

  int a, b, c;
  Rep(i, M) {
    Scd3(a, b, c);
    a--, b--;
    if (c > L)
      continue;
    e[a].push_back({b, c});
    e[b].push_back({a, c});
  }

  priority_queue<pair<pll, int>, vector<pair<pll, int>>,
                 greater<pair<pll, int>>>
      q;
  Rep(i, N) {
    q.push({{0, 0}, i});
    while (q.size()) {
      int from = q.top().second;
      pll dist = q.top().first;
      q.pop();
      if (t[i][from] < dist)
        continue;
      for (edge tt : e[from]) {
        int to = tt.to;
        int di = tt.dis;
        pll old = t[i][to];
        pll ne;
        if (dist.second + di > L) {
          ne = {dist.first + 1, di};
        } else {
          ne = {dist.first, dist.second + di};
        }
        if (ne < old) {
          t[i][to] = ne;
          q.push({ne, to});
        }
      }
    }
  }

  // Rep(i,N) Rep(j,N){
  //     printf ("%lld,%lld%c", t[i][j].first, t[i][j].second, j == N-1 ? '\n' :
  //     ' ' );
  // }

  int Q;
  Scd(Q);

  Rep(i, Q) {
    Scd2(a, b);
    a--, b--;
    printf("%lld\n", t[a][b].first == INF ? -1 : t[a][b].first);
  }

  // Rep(k,N) Rep(i,N) Rep(j,N){
  //     d[i][j] = min( d[i][j] , d[i][k] + d[k][j] );
  // }

  return 0;
}