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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VINT;
typedef unsigned long long ULL;
typedef pair<ULL, int> PAIR;
const ULL INF = 1e16;
struct Graph {
  int N, M;
  VINT B;
  vector<ULL> w, d;
  vector<VINT> from;
  Graph(int n) : N(n), M(0), d(n), from(n) {}
  void setEdge(int s, int t, ULL u) {
    B.push_back(t);
    w.push_back(u);
    from[s].push_back(M);
    M++;
  }
  void Dijkstra(int s) {
    priority_queue<PAIR, vector<PAIR>, greater<PAIR>> Q;
    for (int i = 0; i < N; i++) {
      d[i] = (i == s) ? 0 : INF;
      Q.push(make_pair(d[i], i));
    }
    while (!Q.empty()) {
      PAIR p = Q.top();
      int v = p.second;
      Q.pop();
      if (p.first == INF) {
        break;
      }
      if (d[v] < p.first) {
        continue;
      }
      for (int i = 0; i < from[v].size(); i++) {
        int e = from[v][i], u = B[e];
        ULL nw = d[v] + w[e];
        if (nw < d[u]) {
          d[u] = nw;
          Q.push(make_pair(d[u], u));
        }
      }
    }
  }
};
int main() {
  int MAX = 0, N, M, S, i, j, k;
  cin >> N >> M >> S;
  int U[M], V[M], A[M], C[N];
  ULL B[M], D[N];
  vector<VINT> edge(N);
  for (i = 0; i < M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    U[i]--;
    V[i]--;
    edge[U[i]].push_back(i);
    edge[V[i]].push_back(i);
    MAX = max(MAX, A[i]);
  }
  MAX = MAX * (N - 1);
  S = min(S, MAX);
  for (i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }
  int h = 0;
  map<pair<int, int>, int> F;
  for (i = 0; i < N; i++) {
    for (j = 0; j <= MAX; j++) {
      F[make_pair(i, j)] = h;
      h++;
    }
  }
  Graph G(h);
  for (i = 0; i < N; i++) {
    for (j = 0; j <= MAX; j++) {
      int p = F[make_pair(i, j)], m = min(j + C[i], MAX);
      if (j < MAX) {
        G.setEdge(p, F[make_pair(i, m)], D[i]);
      }
      for (k = 0; k < edge[i].size(); k++) {
        int e = edge[i][k], v = (i != U[e]) ? U[e] : V[e];
        if (A[e] <= j) {
          G.setEdge(p, F[make_pair(v, j - A[e])], B[e]);
        }
      }
    }
  }
  G.Dijkstra(F[make_pair(0, S)]);
  ULL ans = 0;
  for (i = 1; i < N; i++) {
    for (ans = INF, j = 0; j <= MAX; j++) {
      ans = min(ans, G.d[F[make_pair(i, j)]]);
    }
    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VINT;
typedef unsigned long long ULL;
typedef pair<ULL, int> PAIR;
const ULL INF = 1e12;
struct Graph {
  int N, M;
  VINT B;
  vector<ULL> w, d;
  vector<VINT> from;
  Graph(int n) : N(n), M(0), d(n), from(n) {}
  void setEdge(int s, int t, ULL u) {
    B.push_back(t);
    w.push_back(u);
    from[s].push_back(M);
    M++;
  }
  void Dijkstra(int s) {
    priority_queue<PAIR, vector<PAIR>, greater<PAIR>> Q;
    for (int i = 0; i < N; i++) {
      d[i] = (i == s) ? 0 : INF;
      Q.push(make_pair(d[i], i));
    }
    while (!Q.empty()) {
      PAIR p = Q.top();
      int v = p.second;
      Q.pop();
      if (p.first == INF) {
        break;
      }
      if (d[v] < p.first) {
        continue;
      }
      for (int i = 0; i < from[v].size(); i++) {
        int e = from[v][i], u = B[e];
        ULL nw = d[v] + w[e];
        if (nw < d[u]) {
          d[u] = nw;
          Q.push(make_pair(d[u], u));
        }
      }
    }
  }
};
int main() {
  int MAX = 0, N, M, S, i, j, k;
  cin >> N >> M >> S;
  int U[M], V[M], A[M], C[N];
  ULL B[M], D[N];
  vector<VINT> edge(N);
  for (i = 0; i < M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    U[i]--;
    V[i]--;
    edge[U[i]].push_back(i);
    edge[V[i]].push_back(i);
    MAX = max(MAX, A[i]);
  }
  MAX = MAX * (N - 1) + 1;
  S = min(S, MAX - 1);
  for (i = 0; i < N; i++) {
    cin >> C[i] >> D[i];
  }
  int h = 0;
  map<pair<int, int>, int> F;
  for (i = 0; i < N; i++) {
    for (j = 0; j < MAX; j++) {
      F[make_pair(i, j)] = h;
      h++;
    }
  }
  Graph G(h);
  for (i = 0; i < N; i++) {
    for (j = 0; j < MAX; j++) {
      int p = F[make_pair(i, j)];
      ULL t = (MAX - j + C[i] - 2) / C[i];
      for (k = 1; k <= t; k++) {
        int m = min(j + C[i] * k, MAX - 1);
        G.setEdge(p, F[make_pair(i, m)], k * D[i]);
      }
      for (k = 0; k < edge[i].size(); k++) {
        int e = edge[i][k], v = (i != U[e]) ? U[e] : V[e];
        if (A[e] <= j) {
          G.setEdge(p, F[make_pair(v, j - A[e])], B[e]);
        }
      }
    }
  }
  G.Dijkstra(F[make_pair(0, S)]);
  ULL ans = 0;
  for (i = 1; i < N; i++) {
    for (ans = INF, j = 0; j < MAX; j++) {
      ans = min(ans, G.d[F[make_pair(i, j)]]);
    }
    cout << ans << "\\n";
  }
}