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

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

typedef long long ll;
const int mxp = 2500;

int n, m, s;
vector<pair<int, int>> g[50 * (mxp + 1) + 5];
ll d[50 * (mxp + 1) + 5];
int give[55], cost[55];

inline int id(const int &ver, const int &poss) {
  return (ver - 1) * (mxp + 1) + poss;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> s;
  s = min(s, mxp);
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    for (int i = a; i <= mxp; i++) {
      g[id(u, i)].emplace_back(id(v, i - a), b);
      g[id(v, i)].emplace_back(id(u, i - a), b);
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> give[i] >> cost[i];
    give[i] = min(give[i], mxp);
    for (int j = 0; j + give[i] <= mxp; j++) {
      g[id(i, j)].emplace_back(id(i, j + give[i]), cost[i]);
    }
  }
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      pq;
  pq.push(make_pair(0, id(1, s)));
  memset(d, 0x3f, sizeof(d));
  d[id(1, s)] = 0;
  while (!pq.empty()) {
    ll cd = pq.top().first;
    int x = pq.top().second;
    pq.pop();
    if (d[x] < cd)
      continue;
    for (pair<int, int> &p : g[x]) {
      if (d[p.first] > d[x] + p.second) {
        d[p.first] = d[x] + p.second;
        pq.push(make_pair(d[p.first], p.first));
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    ll ans = 1e18;
    for (int j = 0; j <= mxp; j++) {
      ans = min(ans, d[id(i, j)]);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

struct edge {
  int y;
  ll cost, len;
  edge() { y = cost = len = 0; }
  edge(int _y, ll _c, ll _l) {
    y = _y;
    cost = _c;
    len = _l;
  }
};

struct cpos {
  int x;
  ll have, ctime;
  cpos() { x = have = ctime = 0; }
  cpos(int _x, ll _have, ll _time) {
    x = _x;
    have = _have;
    ctime = _time;
  }
  const bool operator<(const cpos &tmp) const {
    if (tmp.ctime != ctime)
      return ctime > tmp.ctime;
    if (tmp.have != have)
      return have < tmp.have;
    return x < tmp.x;
  }
};

int n, m, s;
vector<edge> g[55];
ll give[55], cost[55];
ll dis[55][2505];

ll solve(int dst) {
  priority_queue<cpos> pq;
  pq.push(cpos(1, min(s, 2500), 0));
  memset(dis, 0x3f, sizeof(dis));
  dis[1][min(s, 2500)] = 0;

  while (true) {
    cpos cur = pq.top();
    pq.pop();
    int &x = cur.x;
    ll &chave = cur.have, &ctime = cur.ctime;
    if (dis[x][chave] < ctime)
      continue;
    if (x == dst)
      return ctime;
    for (edge &e : g[x]) {
      ll ntime = ctime + e.len;
      ll nhave = chave;
      if (nhave < e.cost) {
        ntime += (e.cost - nhave + give[x] - 1) / give[x] * cost[x];
        nhave += (e.cost - nhave + give[x] - 1) / give[x] * give[x];
      }
      nhave = min(nhave - e.cost, 2500ll);
      while (nhave <= 2500) {
        if (dis[e.y][nhave] > ntime) {
          for (ll i = nhave; i >= 0 && dis[e.y][i] > ntime; i--) {
            dis[e.y][i] = ntime;
          }
          pq.push(cpos(e.y, nhave, ntime));
        }
        ntime += cost[x];
        nhave += give[x];
      }
    }
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }
  for (int i = 1; i <= n; i++) {
    cin >> give[i] >> cost[i];
  }
  for (int i = 2; i <= n; i++) {
    cout << solve(i) << endl;
  }

  return 0;
}