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

#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
// const ll mod = 1000000007;
const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\\n"

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T> vector<T> dijkstra(WeightedGraph<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);

  using Pi = pair<T, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while (!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    que.pop();
    if (dist[idx] < cost)
      continue;
    for (auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (dist[e.to] <= next_cost)
        continue;
      dist[e.to] = next_cost;
      que.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}

bool solve() {
  ll n, m, s;
  cin >> n >> m >> s;
  ll MA = 5000;
  chmin(s, MA - 1);
  map<pair<ll, ll>, ll> mp;
  int k = 0;
  rep(i, n) rep(j, MA) mp[pair<ll, ll>(i, j)] = k++;

  WeightedGraph<ll> G(n * MA);

  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    rep(j, MA) {
      if (j >= a) {
        G[mp[pair<ll, ll>(u, j)]].emplace_back(mp[pair<ll, ll>(v, j - a)], b);
        G[mp[pair<ll, ll>(v, j)]].emplace_back(mp[pair<ll, ll>(u, j - a)], b);
      }
    }
  }

  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(j, MA) {
      if (j + c < MA)
        G[mp[pair<ll, ll>(i, j)]].emplace_back(mp[pair<ll, ll>(i, j + c)], d);
    }
  }

  auto v = dijkstra(G, mp[pair<ll, ll>(0, s)]);
  rep(i, n) {
    if (i == 0)
      continue;
    ll ans = INF;
    rep(j, MA) chmin(ans, v[mp[pair<ll, ll>(i, j)]]);
    cout << ans << endl;
  }
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(30);
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF = (1LL << 62);
const ld pi = acosl((ld)-1);
// const ll mod = 1000000007;
const ll mod = 1234567;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
#define endn "\\n"

struct Node {
  ll to, cost, time;
};

bool solve() {
  ll n, m, S;
  cin >> n >> m >> S;
  vector<Node> G[n];
  rep(i, m) {
    ll U, V, A, B;
    cin >> U >> V >> A >> B;
    U--;
    V--;
    Node n;
    n.to = V;
    n.cost = A;
    n.time = B;
    G[U].push_back(n);

    n.to = U;
    n.cost = A;
    n.time = B;
    G[V].push_back(n);
  }
  vector<ll> C(n);
  vector<ll> D(n);
  rep(i, n) cin >> C[i] >> D[i];
  ll MA = 3000;
  ll dp[n][MA];
  rep(i, n) rep(j, MA) dp[i][j] = INF;
  rep(i, min(S + 1, MA)) dp[0][i] = 0;

  rep(__, n) rep(_, n) rep(j, MA) rep(i, n) {
    {
      if (dp[i][j] == INF)
        continue;
      // 買う？
      if (j + C[i] < MA)
        chmin(dp[i][j + C[i]], dp[i][j] + D[i]);

      // go
      rep(k, G[i].size()) {
        if (j - G[i][k].cost >= 0)
          chmin(dp[G[i][k].to][j - G[i][k].cost], dp[i][j] + G[i][k].time);
        // cout<<"GO"<<endl;
      }
    }
  }

  rep(i, n) {
    if (i == 0)
      continue;
    ll ans = INF;
    rep(j, MA) chmin(ans, dp[i][j]);
    cout << ans << endl;
  }

  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(30);
  solve();
}