Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;

const int N = 205;
typedef long long ll;

struct Edge {
  int to, next, c;
  ll w;
} e[N << 1];
struct node {
  int pos, res;
  ll len;
  bool operator<(const node &x) const { return x.len < len; }
};
priority_queue<node> q;
int n, m, s, cnt, first[N], maxt;
ll dis[N][5005], c[N], d[N], ans[N];

void add(int u, int v, int c, ll w) {
  e[++cnt] = (Edge){v, first[u], c, w};
  first[u] = cnt;
}

int calc(int x) { return min(x, maxt); }

void Dijkstra() {
  memset(dis, 0x7f, sizeof(dis));
  dis[1][calc(s)] = 0;
  q.push((node){1, calc(s), 0});
  while (!q.empty()) {
    node tem = q.top();
    q.pop();
    int u = tem.pos, res = tem.res;
    ll len = tem.len;
    if (len != dis[u][res])
      continue;
    for (int i = 1; i; i++) {
      int now = calc(res + i * c[u]);
      ll k = i * d[u] + len;
      if (dis[u][now] > k) {
        dis[u][now] = k;
        q.push((node){u, now, k});
      }
      if (now == maxt)
        break;
    }
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      ll k = len + e[i].w;
      if (res < e[i].c)
        continue;
      if (dis[v][res - e[i].c] > k) {
        dis[v][res - e[i].c] = k;
        q.push((node){v, res - e[i].c, k});
      }
    }
  }
  memset(ans, 0x7f, sizeof(ans));
  for (int i = 2; i <= n; i++)
    for (int j = 0; j <= maxt; j++)
      ans[i] = min(ans[i], dis[i][j]);
}

int main() {
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, x;
    ll y;
    cin >> u >> v >> x >> y;
    add(u, v, x, y);
    add(v, u, x, y);
    maxt = max(maxt, x);
  }
  maxt = maxt * m;
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  Dijkstra();
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;

const int N = 205;
typedef long long ll;

struct Edge {
  int to, next, c;
  ll w;
} e[N << 1];
struct node {
  int pos, res;
  ll len;
  bool operator<(const node &x) const { return x.len < len; }
};
priority_queue<node> q;
int n, m, s, cnt, first[N], maxt;
ll dis[N][5005], c[N], d[N], ans[N];

void add(int u, int v, int c, ll w) {
  e[++cnt] = (Edge){v, first[u], c, w};
  first[u] = cnt;
}

int calc(int x) { return min(x, maxt); }

void Dijkstra() {
  memset(dis, 0x7f, sizeof(dis));
  dis[1][calc(s)] = 0;
  q.push((node){1, calc(s), 0});
  while (!q.empty()) {
    node tem = q.top();
    q.pop();
    int u = tem.pos, res = tem.res;
    ll len = tem.len;
    if (len != dis[u][res])
      continue;
    for (int i = 1; i; i++) {
      int now = calc(res + i * c[u]);
      ll k = i * d[u] + len;
      if (dis[u][now] > k) {
        dis[u][now] = k;
        q.push((node){u, now, k});
      }
      if (now == maxt)
        break;
    }
    for (int i = first[u]; i; i = e[i].next) {
      int v = e[i].to;
      ll k = len + e[i].w;
      if (res < e[i].c)
        continue;
      if (dis[v][res - e[i].c] > k) {
        dis[v][res - e[i].c] = k;
        q.push((node){v, res - e[i].c, k});
      }
    }
  }
  memset(ans, 0x7f, sizeof(ans));
  for (int i = 2; i <= n; i++)
    for (int j = 0; j <= maxt; j++)
      ans[i] = min(ans[i], dis[i][j]);
}

int main() {
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int u, v, x;
    ll y;
    cin >> u >> v >> x >> y;
    add(u, v, x, y);
    add(v, u, x, y);
    maxt = max(maxt, x);
  }
  maxt = maxt * (n + 1);
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  Dijkstra();
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}