Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

constexpr int bm = 2500;
ll BIT[50][bm];
int zero[50];
priority_queue<ll> PQ;
inline ll query(int A, int gin) {
  ll kotae = 0;
  while (gin < bm) {
    kotae = max(BIT[A][gin], kotae);
    gin += gin & -gin;
  }
  return kotae;
}
inline void query2(int A, int gin, ll ti) {
  int tmp = gin;
  while (gin < bm) {
    if (BIT[A][gin] > ti)
      return;
    gin += gin & -gin;
  }
  gin = tmp;
  while (gin > 0) {
    if (BIT[A][gin] >= ti)
      break;
    BIT[A][gin] = ti;
    gin -= gin & -gin;
  }
  PQ.push(ti << 18 | A << 12 | tmp);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, S;
  cin >> N >> M >> S;
  if (S >= 2460)
    S = 2460;

  vector<ll> E[50];
  rep(i, M) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    b = a << 8 | b << 16;
    E[u].pb(v | b);
    E[v].pb(u | b);
  }
  int C[50], D[50];
  rep(i, N) { cin >> C[i] >> D[i]; }

  query2(0, S + 1, 4e12);
  ll zenbu = 0;
  ll owari = (1ll << N) - 1;

  while (PQ.size()) {
    auto p = PQ.top();
    PQ.pop();
    ll ti = p >> 18;
    int i = p >> 12 & 63;
    int gin = p & 4095;

    if (ti < query(i, gin))
      continue;

    ll ti2 = ti - D[i];
    int gin2 = gin + C[i];
    if (gin2 > 2460)
      gin2 = 2460;
    query2(i, gin2, ti2);

    for (auto pp : E[i]) {
      int to = pp & 255;
      ti2 = ti - (pp >> 16);
      gin2 = gin - (pp >> 8 & 255);
      if (gin2 > 0)
        query2(to, gin2, ti2);
    }
    zenbu |= 1ll << i;
    if (zenbu == owari)
      break;
  }

  for (int i = 1; i < N; i++) {
    co(ll(4e12 - query(i, 1)));
  }

  Would you please return 0;
}

Fixed submission:
// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

constexpr int bm = 2500;
ll BIT[50][bm];
int zero[50];
priority_queue<ll> PQ;
inline ll query(int A, int gin) {
  ll kotae = 0;
  while (gin < bm) {
    kotae = max(BIT[A][gin], kotae);
    gin += gin & -gin;
  }
  return kotae;
}
inline void query2(int A, int gin, ll ti) {
  int tmp = gin;
  while (gin < bm) {
    if (BIT[A][gin] >= ti)
      return;
    gin += gin & -gin;
  }
  gin = tmp;
  while (gin > 0) {
    if (BIT[A][gin] >= ti)
      break;
    BIT[A][gin] = ti;
    gin -= gin & -gin;
  }
  PQ.push(ti << 18 | A << 12 | tmp);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, S;
  cin >> N >> M >> S;
  if (S >= 2460)
    S = 2460;

  vector<ll> E[50];
  rep(i, M) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    b = a << 8 | b << 16;
    E[u].pb(v | b);
    E[v].pb(u | b);
  }
  int C[50], D[50];
  rep(i, N) { cin >> C[i] >> D[i]; }

  query2(0, S + 1, 4e12);
  ll zenbu = 0;
  ll owari = (1ll << N) - 1;

  while (PQ.size()) {
    auto p = PQ.top();
    PQ.pop();
    ll ti = p >> 18;
    int i = p >> 12 & 63;
    int gin = p & 4095;

    if (ti < query(i, gin))
      continue;

    ll ti2 = ti - D[i];
    int gin2 = gin + C[i];
    if (gin2 > 2460)
      gin2 = 2460;
    query2(i, gin2, ti2);

    for (auto pp : E[i]) {
      int to = pp & 255;
      ti2 = ti - (pp >> 16);
      gin2 = gin - (pp >> 8 & 255);
      if (gin2 > 0)
        query2(to, gin2, ti2);
    }
    zenbu |= 1ll << i;
    if (zenbu == owari)
      break;
  }

  for (int i = 1; i < N; i++) {
    co(ll(4e12 - query(i, 1)));
  }

  Would you please return 0;
}