Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int N = 55;
const int NM = 5e3 + 10;

int n, m, S;
int c[N], d[N];
int a[N][N], b[N][N];
long long f[N][NM], res[N];
vector<int> adj[N];

bool minimize(long long &x, const long long &y) {
  if (x > y)
    x = y;
  else
    return false;
  return true;
}

void dijkstra() {
  memset(f, 60, sizeof(f));
  memset(res, 60, sizeof(res));
  f[1][S] = 0;
  priority_queue<pair<long long, pair<int, int>>> P;
  P.push(mp(0, mp(1, S)));
  while (P.size()) {
    long long dist = -P.top().fi;
    int u = P.top().se.fi, curs = P.top().se.se;
    P.pop();
    if (f[u][curs] != dist)
      continue;
    minimize(res[u], f[u][curs]);
    for (int v : adj[u])
      if (curs >= a[u][v]) {
        if (minimize(f[v][curs - a[u][v]], dist + 1ll * b[u][v]))
          P.push(mp(-f[v][curs - a[u][v]], mp(v, curs - a[u][v])));
      }
    for (int i = 1; i <= 5000; i++) {
      int news, flag = 0;
      if (1ll * i * c[u] + 1ll * curs > (long long)(5e3))
        news = 5000, flag = 1;
      else
        news = i * c[u] + curs;
      if (minimize(f[u][news], dist + 1ll * i * d[u]))
        P.push(mp(-f[u][news], mp(u, news)));
      if (flag)
        break;
    }
  }
}

int main() {
  cin >> n >> m >> S;
  S = min(S, 5000);
  for (int i = 0; i < m; i++) {
    int x, y, z1, z2;
    cin >> x >> y >> z1 >> z2;
    adj[x].push_back(y);
    adj[y].push_back(x);
    a[x][y] = a[y][x] = z1;
    b[x][y] = b[y][x] = z2;
  }
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  dijkstra();
  for (int i = 2; i <= n; i++)
    cout << res[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int N = 55;
const int NM = 5e3 + 10;

int n, m, S;
int c[N], d[N];
int a[N][N], b[N][N];
long long f[N][NM], res[N];
vector<int> adj[N];

bool minimize(long long &x, const long long &y) {
  if (x > y)
    x = y;
  else
    return false;
  return true;
}

void dijkstra() {
  memset(f, 60, sizeof(f));
  memset(res, 60, sizeof(res));
  f[1][S] = 0;
  priority_queue<pair<long long, pair<int, int>>> P;
  P.push(mp(0, mp(1, S)));
  while (P.size()) {
    long long dist = -P.top().fi;
    int u = P.top().se.fi, curs = P.top().se.se;
    P.pop();
    if (f[u][curs] != dist)
      continue;
    minimize(res[u], f[u][curs]);
    for (int v : adj[u])
      if (curs >= a[u][v]) {
        if (minimize(f[v][curs - a[u][v]], dist + 1ll * b[u][v]))
          P.push(mp(-f[v][curs - a[u][v]], mp(v, curs - a[u][v])));
      }
    for (int i = 1; i <= 5000; i++) {
      int news, flag = 0;
      if (1ll * i * c[u] + 1ll * curs > (long long)(5e3))
        news = 5000, flag = 1;
      else
        news = i * c[u] + curs;
      if (minimize(f[u][news], dist + 1ll * i * d[u]))
        P.push(mp(-f[u][news], mp(u, news)));
      else
        break;
      if (flag)
        break;
    }
  }
}

int main() {
  cin >> n >> m >> S;
  S = min(S, 5000);
  for (int i = 0; i < m; i++) {
    int x, y, z1, z2;
    cin >> x >> y >> z1 >> z2;
    adj[x].push_back(y);
    adj[y].push_back(x);
    a[x][y] = a[y][x] = z1;
    b[x][y] = b[y][x] = z2;
  }
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> d[i];
  dijkstra();
  for (int i = 2; i <= n; i++)
    cout << res[i] << endl;
}