Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N, M, S;
  IN(N, M, S);
  vector<vector<pair<ll, pair<ll, ll>>>> Graph(N);
  REP(i, M) {
    ll U, V, A, B;
    IN(U, V, A, B);
    U--;
    V--;
    pair<ll, pair<ll, ll>> tmp1;
    tmp1.first = V;
    tmp1.second.first = A;
    tmp1.second.second = B;
    Graph[U].pb(tmp1);
    tmp1.first = U;
    Graph[V].pb(tmp1);
  }

  vpll CD(N);
  REP(i, N) { IN(CD[i].first, CD[i].second); }

  priority_queue<pair<ll, pair<ll, ll>>> que;
  // time, pos, money
  que.push(make_pair(0, make_pair(0, S)));
  vll finish(N, -1);
  ll count = 0;

  vvb state(N, vb(2500, false));
  vll money_max(N, -1);

  while (que.size()) {
    pair<ll, pair<ll, ll>> tmp = que.top();
    que.pop();

    ll time = -tmp.first;
    ll pos = tmp.second.first;
    ll money = tmp.second.second;

    if (money < 0)
      continue;

    if (finish[pos] < 0) {
      finish[pos] = time;
      count++;

      if (count == N)
        break;
    }

    if (money_max[pos] >= money)
      continue;

    chmax(money_max[pos], money);

    if (money < 2500) {
      if (state[pos][money])
        continue;
      state[pos][money] = true;
    }

    if (money < 2500) {
      // case1
      ll time1 = time + CD[pos].second;
      ll money1 = money + CD[pos].first;
      ll pos1 = pos;
      que.push(make_pair(-time1, make_pair(pos1, money1)));
    }

    // case2
    REP(i, Graph[pos].size()) {
      pair<ll, pair<ll, ll>> n_tmp = Graph[pos][i];
      ll n_pos = n_tmp.first;
      ll A_tmp = n_tmp.second.first;
      ll B_tmp = n_tmp.second.second;

      ll time2 = time + B_tmp;
      ll money2 = money - A_tmp;
      if (money2 < 0)
        continue;

      que.push(make_pair(-time2, make_pair(n_pos, money2)));
    }
  }

  FOR(i, 1, N) { OUT(finish[i]); }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N, M, S;
  IN(N, M, S);
  vector<vector<pair<ll, pair<ll, ll>>>> Graph(N);
  REP(i, M) {
    ll U, V, A, B;
    IN(U, V, A, B);
    U--;
    V--;
    pair<ll, pair<ll, ll>> tmp1;
    tmp1.first = V;
    tmp1.second.first = A;
    tmp1.second.second = B;
    Graph[U].pb(tmp1);
    tmp1.first = U;
    Graph[V].pb(tmp1);
  }

  vpll CD(N);
  REP(i, N) { IN(CD[i].first, CD[i].second); }

  priority_queue<pair<ll, pair<ll, ll>>> que;
  // time, pos, money
  que.push(make_pair(0, make_pair(0, S)));
  vll finish(N, -1);
  finish[0] = 0;
  ll count = 1;

  vvb state(N, vb(2500, false));
  vll money_max(N, 0);
  money_max[0] = S;

  while (que.size()) {
    pair<ll, pair<ll, ll>> tmp = que.top();
    que.pop();

    ll time = -tmp.first;
    ll pos = tmp.second.first;
    ll money = tmp.second.second;

    if (money < 0)
      continue;
    if (finish[pos] < 0) {
      finish[pos] = time;
      count++;
    }
    if (count == N)
      break;

    chmax(money_max[pos], money);

    if (money < 2500) {
      if (state[pos][money])
        continue;
      state[pos][money] = true;
    }

    if (money < 2500) {
      // case1
      ll time1 = time + CD[pos].second;
      ll money1 = money + CD[pos].first;
      if (money1 <= money_max[pos]) {
        ll c = (money_max[pos] - money1) / CD[pos].first;
        if (c * CD[pos].first + money1 <= money_max[pos])
          c++;
        money1 += c * CD[pos].first;
        time1 += c * CD[pos].second;
      }
      ll pos1 = pos;
      que.push(make_pair(-time1, make_pair(pos1, money1)));
    }

    // case2
    REP(i, Graph[pos].size()) {
      pair<ll, pair<ll, ll>> n_tmp = Graph[pos][i];
      ll n_pos = n_tmp.first;
      ll A_tmp = n_tmp.second.first;
      ll B_tmp = n_tmp.second.second;

      ll time2 = time + B_tmp;
      ll money2 = money - A_tmp;
      if (money2 < 0)
        continue;

      que.push(make_pair(-time2, make_pair(n_pos, money2)));
    }
  }

  FOR(i, 1, N) { OUT(finish[i]); }

  return 0;
}