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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;

int N;
#define index(a, b) ((a)*50 * N + (b))
struct node {
  vector<int> to;
  vector<int> cost;
  ll min_cost;
  vector<int> path;
  node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
  priority_queue<P, vector<P>, greater<P>> q;
  nodes[start_index].min_cost = 0;
  q.push(P(0, start_index));
  while (q.size() != 0) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (nodes[v].min_cost < p.first)
      continue;
    for (int i = 0; i < nodes[v].to.size(); i++) {
      int to = nodes[v].to[i];
      ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
      if (nodes[to].min_cost > tmp_cost) {
        nodes[to].min_cost = tmp_cost;
        q.push(P(nodes[to].min_cost, to));
      }
    }
  }
}

int main() {
  int M, S;
  cin >> N >> M >> S;
  S = min(S, 50 * N - 1);
  vector<node> nodes(N * 50 * N);
  for (int i = 0; i < M; i++) {
    int U, V, A, B;
    cin >> U >> V >> A >> B;
    for (int j = 50 * N - 1; j - A >= 0; j--) {
      nodes[index(U - 1, j)].to.push_back(index(V - 1, j - A));
      nodes[index(V - 1, j)].to.push_back(index(U - 1, j - A));
      nodes[index(U - 1, j)].cost.push_back(B);
      nodes[index(V - 1, j)].cost.push_back(B);
    }
  }
  for (int i = 0; i < N; i++) {
    int C, D;
    cin >> C >> D;
    for (int j = 0; j < 50 * N; j++) {
      nodes[index(i, j)].to.push_back(index(i, min(j + C, 50 * N - 1)));
      nodes[index(i, j)].cost.push_back(D);
    }
  }

  dijkstra(S, nodes);
  for (int i = 1; i < N; i++) {
    ll ans = INF;
    for (int j = 0; j < 50 * N; j++) {
      ans = min(ans, nodes[index(i, j)].min_cost);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;

int N;
#define index(a, b) ((a)*50 * N + (b))
struct node {
  vector<int> to;
  vector<int> cost;
  ll min_cost;
  vector<int> path;
  node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
  priority_queue<P, vector<P>, greater<P>> q;
  nodes[start_index].min_cost = 0;
  q.push(P(0, start_index));
  while (q.size() != 0) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (nodes[v].min_cost < p.first)
      continue;
    for (int i = 0; i < nodes[v].to.size(); i++) {
      int to = nodes[v].to[i];
      ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
      if (nodes[to].min_cost > tmp_cost) {
        nodes[to].min_cost = tmp_cost;
        q.push(P(nodes[to].min_cost, to));
      }
    }
  }
}

int main() {
  int M, S;
  cin >> N >> M >> S;
  S = min(S, 50 * N - 1);
  vector<node> nodes(N * 50 * N);
  for (int i = 0; i < M; i++) {
    int U, V, A, B;
    cin >> U >> V >> A >> B;
    for (int j = 50 * N - 1; j - A >= 0; j--) {
      nodes[index(U - 1, j)].to.push_back(index(V - 1, j - A));
      nodes[index(V - 1, j)].to.push_back(index(U - 1, j - A));
      nodes[index(U - 1, j)].cost.push_back(B);
      nodes[index(V - 1, j)].cost.push_back(B);
    }
  }
  for (int i = 0; i < N; i++) {
    int C, D;
    cin >> C >> D;
    for (int j = 0; j < 50 * N; j++) {
      nodes[index(i, j)].to.push_back(index(i, min(j + C, 50 * N - 1)));
      nodes[index(i, j)].cost.push_back(D);
    }
  }

  dijkstra(S, nodes);
  for (int i = 1; i < N; i++) {
    ll ans = INF;
    for (int j = 0; j < 50 * N; j++) {
      ans = min(ans, nodes[index(i, j)].min_cost);
    }
    cout << ans << endl;
  }
}