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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

struct edge {
  ll to, cost, t;
};

struct state {
  ll d, city, ag;
};

bool operator>(const state &a, const state &b) { return a.d > b.d; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, s;
  cin >> n >> m >> s;
  vector<vector<edge>> g(n);
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
  }
  vector<ll> c(n), d(n);
  rep(i, n) { cin >> c[i] >> d[i]; }
  ll ma = 3000;
  chmin(s, ll(ma));
  vector<vector<ll>> dist(n, vector<ll>(10001, inf)); // j==maは銀貨無限枚
  priority_queue<state, vector<state>, greater<state>> que;
  dist[0][s] = 0;
  que.push({0, 0, s});
  while (!que.empty()) {
    auto now = que.top();
    que.pop();
    ll v = now.city, ag = now.ag;
    if (dist[v][ag] < now.d)
      continue;
    ll nag = min(ag + c[v], ma);
    if (chmin(dist[v][nag], dist[v][ag] + d[v])) {
      que.push({dist[v][nag], v, nag});
    }
    for (auto to : g[v]) {
      if (ag >= to.cost) {
        if (chmin(dist[to.to][ag - to.cost], dist[v][ag] + to.t)) {
          que.push({dist[to.to][ag - to.cost], to.to, ag - to.cost});
        }
      }
    }
  }
  FOR(i, 1, n) {
    ll ans = inf;
    rep(j, ma + 1) chmin(ans, dist[i][j]);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

struct edge {
  ll to, cost, t;
};

struct state {
  ll d, city, ag;
};

bool operator>(const state &a, const state &b) { return a.d > b.d; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m, s;
  cin >> n >> m >> s;
  vector<vector<edge>> g(n);
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
  }
  vector<ll> c(n), d(n);
  rep(i, n) { cin >> c[i] >> d[i]; }
  ll ma = 3000;
  chmin(s, ll(ma));
  vector<vector<ll>> dist(n, vector<ll>(10001, inf)); // j==maは銀貨無限枚
  priority_queue<state, vector<state>, greater<state>> que;
  dist[0][s] = 0;
  que.push({0, 0, s});
  while (!que.empty()) {
    auto now = que.top();
    que.pop();
    ll v = now.city, ag = now.ag;
    if (dist[v][ag] < now.d)
      continue;
    for (auto to : g[v]) {
      ll nag = ag;
      ll nt = to.t;
      if (nag == ma) {
        if (chmin(dist[to.to][nag], dist[v][ag] + nt)) {
          que.push({dist[to.to][nag], to.to, nag});
        }
        continue;
      }
      if (nag >= to.cost) {
        if (chmin(dist[to.to][nag - to.cost], dist[v][ag] + nt)) {
          que.push({dist[to.to][nag - to.cost], to.to, nag - to.cost});
        }
      }
      while (nag < ma) {
        nag += c[v];
        chmin(nag, ll(ma));
        nt += d[v];
        if (nag >= to.cost) {
          if (chmin(dist[to.to][nag - to.cost], dist[v][ag] + nt)) {
            que.push({dist[to.to][nag - to.cost], to.to, nag - to.cost});
          }
        }
      }
    }
  }
  FOR(i, 1, n) {
    ll ans = inf;
    rep(j, ma + 1) chmin(ans, dist[i][j]);
    cout << ans << endl;
  }
}