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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
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;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, s;
  cin >> n >> m >> s;

  vvll distcur(n, vll(n, HIGHINF)), distmv(n, vll(n, HIGHINF));
  for (ll i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    distcur[u][v] = distcur[v][u] = -a, distmv[u][v] = distmv[v][u] = b;
  }
  for (ll i = 0; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    distcur[i][i] = c, distmv[i][i] = d;
  }

  vvll dp(n, vll(2505, HIGHINF));
  dp[0][min(s, 2500LL)] = 0;
  for (ll ttttt = 0; ttttt < 100; ttttt++) {
    for (ll i = 0; i < n; i++) {
      for (ll j = 0; j < 2501; j++) {
        for (ll toi = 0; toi < n; toi++) {
          if (i == toi) {
            chmin(dp[i][min(2500LL, j + distcur[i][i])],
                  dp[i][j] + distmv[i][i]);
          } else {
            if (distcur[i][toi] != HIGHINF && j + distcur[i][toi] >= 0) {
              chmin(dp[toi][j + distcur[i][toi]], dp[i][j] + distmv[i][toi]);
            }
          }
        }
      }
    }
  }

  vll ans(n, HIGHINF);
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2501; j++)
      chmin(ans[i], dp[i][j]);
  }

  for (ll i = 1; i < n; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
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;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, s;
  cin >> n >> m >> s;

  vvll distcur(n, vll(n, HIGHINF)), distmv(n, vll(n, HIGHINF));
  for (ll i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    distcur[u][v] = distcur[v][u] = -a, distmv[u][v] = distmv[v][u] = b;
  }
  for (ll i = 0; i < n; i++) {
    ll c, d;
    cin >> c >> d;
    distcur[i][i] = c, distmv[i][i] = d;
  }

  vvll dp(n, vll(2505, HIGHINF));
  dp[0][min(s, 2500LL)] = 0;
  for (ll ttttt = 0; ttttt < 500; ttttt++) {
    for (ll i = 0; i < n; i++) {
      for (ll j = 0; j < 2501; j++) {
        chmin(dp[i][min(2500LL, j + distcur[i][i])], dp[i][j] + distmv[i][i]);
      }
      for (ll j = 0; j < 2501; j++) {
        for (ll toi = 0; toi < n; toi++) {
          if (i == toi)
            continue;
          if (distcur[i][toi] != HIGHINF && j + distcur[i][toi] >= 0) {
            chmin(dp[toi][j + distcur[i][toi]], dp[i][j] + distmv[i][toi]);
          }
        }
      }
    }
  }

  vll ans(n, HIGHINF);
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2501; j++)
      chmin(ans[i], dp[i][j]);
  }

  for (ll i = 1; i < n; i++)
    cout << ans[i] << '\\n';
  return 0;
}