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

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef tuple<ll, ll, ll> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.141592653589793238462643383279;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef tuple<ll, int, ll> P;

bool operator>(P a, P b) { return get<0>(a) > get<0>(b); }

int main(void) {
  ll N, M, S;
  cin >> N >> M >> S;
  vector<tpl> edge[N];
  ll mxa = 0;
  REP(i, M) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    edge[u].emplace_back(v, a, b);
    edge[v].emplace_back(u, a, b);
    chmax(mxa, a);
  }
  VL C(N), D(N);
  REP(i, N) cin >> C[i] >> D[i];

  ll K = mxa * (N - 1);
  ll dp[N][K + 1];
  REP(i, N) REP(j, K + 1) dp[i][j] = LINF;

  priority_queue<tpl, vector<tpl>, greater<tpl>> q;

  q.push(tpl(0, 0, min(K, S)));
  while (q.size()) {
    tpl p = q.top();
    q.pop();
    ll n = get<0>(p), city = get<1>(p), s = get<2>(p);
    if (chmin(dp[city][s], n)) {
      for (auto e : edge[city]) {
        int c = get<0>(e);
        ll cost = get<2>(e), news = s - get<1>(e);
        if (news >= 0)
          q.push(tpl(dp[city][s] + cost, c, news));
      }
      q.push(tpl(dp[city][s] + D[city], city, min(K, s + C[city])));
    }
  }

  REP(i, N - 1) {
    ll ans = LINF;
    REP(j, K + 1) chmin(ans, dp[i + 1][j]);
    cout << ans << en;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef tuple<ll, ll, ll> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.141592653589793238462643383279;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef tuple<ll, int, ll> P;

bool operator>(P a, P b) { return get<0>(a) > get<0>(b); }

int main(void) {
  ll N, M, S;
  cin >> N >> M >> S;
  vector<tpl> edge[N];
  ll mxa = 0;
  REP(i, M) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    edge[u].emplace_back(v, a, b);
    edge[v].emplace_back(u, a, b);
    chmax(mxa, a);
  }
  VL C(N), D(N);
  REP(i, N) cin >> C[i] >> D[i];

  ll K = mxa * (N - 1);
  ll dp[N][K + 1];
  REP(i, N) REP(j, K + 1) dp[i][j] = LINF;

  priority_queue<tpl, vector<tpl>, greater<tpl>> q;

  q.push(tpl(0, 0, min(K, S)));
  while (q.size()) {
    tpl p = q.top();
    q.pop();
    ll n = get<0>(p), city = get<1>(p), s = get<2>(p);
    if (chmin(dp[city][s], n)) {
      for (auto e : edge[city]) {
        int c = get<0>(e);
        REP(i, K + 1) {
          ll cost = get<2>(e) + i * D[city], news = s - get<1>(e) + i * C[city];
          if (news >= 0 && dp[c][min((ll)K, news)] == LINF)
            q.push(tpl(dp[city][s] + cost, c, min((ll)K, news)));
          if (news > K)
            break;
        }
      }
    }
  }

  REP(i, N - 1) {
    ll ans = LINF;
    REP(j, K + 1) chmin(ans, dp[i + 1][j]);
    cout << ans << en;
  }
  return 0;
}