Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 1000000000000000LL
#define INF 200000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll modpow(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll t = modpow(a, b / 2, m);
  if (b & 1) {
    return (t * t % m) * a % m;
  } else {
    return t * t % m;
  }
}
struct edge {
  int to;
  ll cost;
  edge(int t, ll c) { to = t, cost = c; }
};

#define MAX_S 5000LL

vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
  priority_queue<P, vector<P>, greater<P>> que;
  vector<ll> dist(n, INF_LL);
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    rep(i, (int)g[v].size()) {
      edge e = g[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  vector<vector<edge>> g(n * (MAX_S + 1));
  rep(i, m) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    REP(j, a, MAX_S + 1) {
      g[(u - 1) * (MAX_S + 1) + j].push_back(
          edge((v - 1) * (MAX_S + 1) + j - a, b));
      g[(v - 1) * (MAX_S + 1) + j].push_back(
          edge((u - 1) * (MAX_S + 1) + j - a, b));
    }
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(j, MAX_S) {
      g[i * (MAX_S + 1) + j].push_back(
          edge(i * (MAX_S + 1) + min(j + c, MAX_S), d));
    }
  }
  vl d(n * (MAX_S + 1));
  d = dijkstra(g, min((ll)s, MAX_S), n * (MAX_S + 1));
  REP(j, 1, n) {
    ll ans = INF_LL;
    rep(i, MAX_S + 1) { chmin(ans, d[j * (MAX_S + 1) + i]); }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 1000000000000000LL
#define INF 200000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll modpow(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll t = modpow(a, b / 2, m);
  if (b & 1) {
    return (t * t % m) * a % m;
  } else {
    return t * t % m;
  }
}
struct edge {
  int to;
  ll cost;
  edge(int t, ll c) { to = t, cost = c; }
};

#define MAX_S 2500LL

vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
  priority_queue<P, vector<P>, greater<P>> que;
  vector<ll> dist(n, INF_LL);
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    rep(i, (int)g[v].size()) {
      edge e = g[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  vector<vector<edge>> g(n * (MAX_S + 1));
  rep(i, m) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    REP(j, a, MAX_S + 1) {
      g[(u - 1) * (MAX_S + 1) + j].push_back(
          edge((v - 1) * (MAX_S + 1) + j - a, b));
      g[(v - 1) * (MAX_S + 1) + j].push_back(
          edge((u - 1) * (MAX_S + 1) + j - a, b));
    }
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(j, MAX_S) {
      g[i * (MAX_S + 1) + j].push_back(
          edge(i * (MAX_S + 1) + min(j + c, MAX_S), d));
    }
  }
  vl d(n * (MAX_S + 1));
  d = dijkstra(g, min((ll)s, MAX_S), n * (MAX_S + 1));
  REP(j, 1, n) {
    ll ans = INF_LL;
    rep(i, MAX_S + 1) { chmin(ans, d[j * (MAX_S + 1) + i]); }
    cout << ans << endl;
  }
  return 0;
}