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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(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;
}
typedef long long ll;
typedef pair<ll, ll> pint;
const long long INF = 1LL << 60;
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  vector<pint> E[50];
  ll dist[50][3010];
  int a[101], b[101], c[50], d[50];
  using Edge = pair<ll, pair<ll, ll>>; // 行先，お金,時間
  using Graph = vector<vector<Edge>>;
  using vertex = vector<pair<ll, ll>>; // 枚数，時間
  Graph G(50);
  vertex V(50);
  priority_queue<pair<ll, pint>, vector<pair<ll, pint>>,
                 greater<pair<ll, pint>>>
      que;
  bool visited[50][3010];
  ll ans[50];
  ll n, m, s;
  cin >> n >> m >> s;
  s = min(s, 3000LL);
  // cout<<s<<endl;
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    G[u - 1].push_back(Edge(v - 1, {a, b}));
    G[v - 1].push_back(Edge(u - 1, {a, b}));
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    V[i] = {c, d};
  }
  rep(i, n) rep(j, 3010) dist[i][j] = INF;
  rep(i, n) rep(j, 3010) visited[i][j] = false;
  dist[0][s] = 0;
  visited[0][s] = true;
  que.push({0LL, {0, s}});
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int v = p.second.first;
    // cout<<v<<endl;
    ll costt = p.first;
    ll costm = p.second.second;
    if (dist[v][costm] < costt)
      continue;
    dist[v][costm] = costt;
    // cout<<dist[v][costm]<<endl;
    auto ver = V[v];
    auto e = G[v];
    // cout<<v<<endl;
    for (int i = 0; i < e.size(); i++) {
      int to = e[i].first;
      ll money = e[i].second.first;
      ll time = e[i].second.second;
      if (money <= costm && (time + costt) < dist[to][costm - money]) {
        que.push({time + costt, {to, costm - money}});
        dist[to][costm - money] = time + costt;
      }
    }
    if (costm + ver.first < 3010 &&
        costt + ver.second < dist[v][costm + ver.first]) {
      que.push({costt + ver.second, {v, costm + ver.first}});
      dist[v][costm + ver.first] = ver.second + costt;
    }
  }
  REP(i, 1, n) {
    ll res = INF;
    rep(j, 3010) { res = min(res, dist[i][j]); }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(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;
}
typedef long long ll;
typedef pair<ll, ll> pint;
const long long INF = 1LL << 60;
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  vector<pint> E[50];
  ll dist[50][3010];
  int a[101], b[101], c[50], d[50];
  using Edge = pair<ll, pair<ll, ll>>; // 行先，お金,時間
  using Graph = vector<vector<Edge>>;
  using vertex = vector<pair<ll, ll>>; // 枚数，時間
  Graph G(50);
  vertex V(50);
  priority_queue<pair<ll, pint>, vector<pair<ll, pint>>,
                 greater<pair<ll, pint>>>
      que;
  bool visited[50][3010];
  ll ans[50];
  ll n, m, s;
  cin >> n >> m >> s;
  s = min(s, 3010LL);
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    G[u - 1].push_back(Edge(v - 1, {a, b}));
    G[v - 1].push_back(Edge(u - 1, {a, b}));
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    V[i] = {c, d};
  }
  rep(i, n) rep(j, 3010) dist[i][j] = INF;
  rep(i, n) rep(j, 3010) visited[i][j] = false;
  dist[0][s] = 0;
  visited[0][s] = true;
  que.push({0LL, {0LL, s}});
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int v = p.second.first;
    // cout<<v<<endl;
    ll costt = p.first;
    ll costm = p.second.second;
    if (dist[v][costm] < costt)
      continue;
    dist[v][costm] = costt;
    // cout<<dist[v][costm]<<endl;
    auto ver = V[v];
    auto e = G[v];
    // cout<<v<<endl;
    for (int i = 0; i < e.size(); i++) {
      int to = e[i].first;
      int money = e[i].second.first;
      int time = e[i].second.second;
      if (money <= costm && time + costt < dist[to][costm - money])
        que.push({time + costt, {to, costm - money}});
    }
    if (costm + ver.first < 3010 && costt < dist[v][costm + ver.first])
      que.push({costt + ver.second, {v, costm + ver.first}});
  }
  REP(i, 1, n) {
    ll res = INF;
    rep(j, 3010) { res = min(res, dist[i][j]); }
    cout << res << endl;
  }
}