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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;
typedef std::vector<long long> vl;
typedef std::vector<std::vector<long long>> vvl;
#define out(x) cout << x << "\\n";
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

ll dp[55][2550];
vector<int> c;
vector<ll> d;
int n, m;
ll s;

template <typename T> struct edge {
  int to;
  T cost;
  edge(int to, T cost) : to(to), cost(cost) {}
};

template <typename T>
vector<vector<ll>> dijkstra(vector<vector<edge<T>>> &g) { // sは始点
  const auto INFTY =
      numeric_limits<ll>::max() /
      2; // 2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<vector<ll>> dist(n, vector<ll>(2505, INFTY));
  using P = pair<T, int>; // firstは最短距離,secondは頂点の番号
  const int MAXV = 2500;
  priority_queue<P, vector<P>, greater<P>> pq;
  dist[0][s] = 0;
  pq.emplace(T{dist[0][s], s}, 0);
  // first.first:時間,first.second:銀貨,secod:頂点
  while (!pq.empty()) {
    auto x = pq.top();
    pq.pop();
    int v = x.second;
    int silver = x.first.second;
    ll time = x.first.first;
    if (dist[v][silver] < time) {
      continue; // 最短距離でなければ飛ばす
    }
    if (silver + c[v] <= MAXV) {
      if (chmin(dist[v][silver + c[v]], time + d[v])) {
        pq.emplace(T{dist[v][silver + c[v]], silver + c[v]}, v);
      }
    }
    for (auto e : g[v]) {
      if ((silver - e.cost.second >= 0) &&
          (chmin(dist[e.to][silver - e.cost.second],
                 dist[v][silver] + e.cost.first))) {
        pq.emplace(
            T{dist[e.to][silver - e.cost.second], silver - e.cost.second},
            e.to);
      }
    }
  }
  return dist;
}

using P = pair<ll, ll>;
void solve() {
  cin >> n >> m >> s;
  chmin(s, 2500LL);
  vector<vector<edge<P>>> g(n);
  rep(i, m) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].eb(v, P{b, a});
    g[v].eb(u, P{b, a});
  }
  c.resize(n);
  d.resize(n);
  rep(i, n) {
    cin >> c[i] >> d[i];
    chmin(c[i], 2500);
  }
  auto e = dijkstra(g);
  REP(i, 1, n) {
    ll ans = INF;
    rep(j, 2501) { chmin(ans, e[i][j]); }
    cout << ans << "\\n";
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;
typedef std::vector<long long> vl;
typedef std::vector<std::vector<long long>> vvl;
#define out(x) cout << x << "\\n";
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

ll dp[55][2550];
vector<int> c;
vector<ll> d;
int n, m;
ll s;

template <typename T> struct edge {
  int to;
  T cost;
  edge(int to, T cost) : to(to), cost(cost) {}
};

template <typename T>
vector<vector<ll>> dijkstra(vector<vector<edge<T>>> &g) { // sは始点
  const auto INFTY =
      numeric_limits<ll>::max() /
      2; // 2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<vector<ll>> dist(n, vector<ll>(2505, INFTY));
  using P = pair<T, int>; // firstは最短距離,secondは頂点の番号
#define MAXV 2500
  priority_queue<P, vector<P>, greater<P>> pq;
  dist[0][s] = 0;
  pq.emplace(T{dist[0][s], s}, 0);
  // first.first:時間,first.second:銀貨,secod:頂点
  while (!pq.empty()) {
    auto x = pq.top();
    pq.pop();
    int v = x.second;
    int silver = x.first.second;
    ll time = x.first.first;
    if (dist[v][silver] < time) {
      continue; // 最短距離でなければ飛ばす
    }
    if (silver + c[v] <= MAXV) {
      if (chmin(dist[v][silver + c[v]], time + d[v])) {
        pq.emplace(T{dist[v][silver + c[v]], silver + c[v]}, v);
      }
    }
    for (auto e : g[v]) {
      rep(i, MAXV + 1) if ((i - e.cost.second >= 0) &&
                           (chmin(dist[e.to][i - e.cost.second],
                                  dist[v][i] + e.cost.first))) {
        pq.emplace(T{dist[e.to][i - e.cost.second], i - e.cost.second}, e.to);
      }
    }
  }
  return dist;
}

using P = pair<ll, ll>;
void solve() {
  cin >> n >> m >> s;
  chmin(s, 2500LL);
  vector<vector<edge<P>>> g(n);
  rep(i, m) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].eb(v, P{b, a});
    g[v].eb(u, P{b, a});
  }
  c.resize(n);
  d.resize(n);
  rep(i, n) {
    cin >> c[i] >> d[i];
    chmin(c[i], 2500);
  }
  auto e = dijkstra(g);
  REP(i, 1, n) {
    ll ans = INF;
    rep(j, 2501) { chmin(ans, e[i][j]); }
    cout << ans << "\\n";
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}