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

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

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>

const int inf = 1e16;

const int N = 51;
vector<pair<int, pii>> g[N];
int n, m, s;
int c[N];
int d[N];
int dp[N][(N * N)];
int dis[N][N];

void build() {
  f(i, n) f(j, n) dis[i][j] = inf;
  f(i, n) dis[i][i] = 0;

  f(i, n) for (auto x : g[i]) dis[i][x.F] = x.S.S;

  f(i, n) f(j, n) f(k, n) dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
}

void solve() {
  cin >> n >> m >> s;

  int u, v, w, x;

  f(i, m) {
    cin >> u >> v >> w >> x;
    u--;
    v--;

    g[u].pb({v, {w, x}});
    g[v].pb({u, {w, x}});
  }

  f(i, n) cin >> c[i] >> d[i];

  build();

  if (s >= 2500) {
    for (int i = 1; i < n; i++)
      cout << dis[0][i] << '\\n';
    return;
  }

  f(i, n) f(j, 2501) dp[i][j] = inf;
  dp[0][s] = 0;

  f(go, n + 1) for (int i = 0; i < n; i++) {
    for (auto x : g[i])
      for (int j = 0; j <= 2500; j++) {
        if (j - x.S.F >= 0)
          dp[i][j - x.S.F] = min(dp[i][j - x.S.F], dp[x.F][j] + x.S.S);
      }

    for (int j = 0; j <= 2500; j++) {
      if (j - c[i] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i][j - c[i]] + d[i]);
      }
    }
  }

  for (int i = 1; i < n; i++) {
    int res = inf;

    for (int j = 0; j <= 2500; j++)
      res = min(res, dp[i][j]);

    cout << res << '\\n';
  }
}

signed main() {
  fast;
  solve();
}

Error: TLE

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)

const int MOD = 1e9 + 7;

int mod_pow(int a, int b, int M = MOD) {
  int res = 1;

  while (b) {
    if (b & 1)
      res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }

  return res;
}

const int inf = 1e16;

const int N = 51;
vector<pair<int, pii>> g[N];
int n, m, s;
int c[N];
int d[N];
int dp[N][(N * N)];
int dis[N][N];

void build() {
  f(i, n) f(j, n) dis[i][j] = inf;
  f(i, n) dis[i][i] = 0;

  f(i, n) for (auto x : g[i]) dis[i][x.F] = x.S.S;

  f(i, n) f(j, n) f(k, n) dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
}

void solve() {
  cin >> n >> m >> s;

  int u, v, w, x;

  f(i, m) {
    cin >> u >> v >> w >> x;
    u--;
    v--;

    g[u].pb({v, {w, x}});
    g[v].pb({u, {w, x}});
  }

  f(i, n) cin >> c[i] >> d[i];

  build();

  if (s >= 2500) {
    for (int i = 1; i < n; i++)
      cout << dis[0][i] << '\\n';
    return;
  }

  f(i, n) f(j, 2501) dp[i][j] = inf;

  dp[0][s] = 0;

  int cnt = s;
  int tm = 0;

  while (cnt <= 2500) {
    cnt += c[0];
    tm += d[0];

    if (cnt <= 2500) {
      dp[0][cnt] = tm;
    }
  }

  f(go, n + 1) for (int i = 0; i < n; i++) {
    for (auto x : g[i])
      for (int j = 0; j <= 2500; j++) {
        if (j - x.S.F >= 0)
          dp[i][j - x.S.F] = min(dp[i][j - x.S.F], dp[x.F][j] + x.S.S);
      }

    for (int j = 0; j <= 2500; j++) {
      int back = j;
      int tmr = 0;

      while (back >= 0) {
        back -= c[i];
        tmr += d[i];

        if (back >= 0)
          dp[i][j] = min(dp[i][j], dp[i][back] + tmr);
      }
    }
  }

  for (int i = 1; i < n; i++) {
    int res = inf;

    for (int j = 0; j <= 2500; j++)
      res = min(res, dp[i][j]);

    cout << res << '\\n';
  }
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}