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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x7f
#define MAX_INF_VAL 0x7f7f7f7f
#define pi 3.141592653589
#define eps 1e-10
// #define p 2173412051LL
#define sz 2

using namespace std;

struct p {
  int u, m;
  ll t;
};
struct cmp {
  bool operator()(const p &a, const p &b) {
    if (a.t == b.t)
      return a.m < b.m;
    return a.t > b.t;
  }
};
priority_queue<p, vector<p>, cmp> q;
ll f[51][5050];
vector<int> e[51];
ll cst[51][51], mins[51][51];
ll c[51], d[51];
int ma;

void dijk(int, int, ll);
void update(int, int, ll);

int main() {
  int n, mm, s, x, y;
  ll a, b;
  ll u, m, t;
  ll ans;
  scanf("%d%d%d", &n, &mm, &s);
  ma = mm * 50;
  s = min(s, ma);
  for (int i = 0; i < mm; ++i) {
    scanf("%d%d%lld%lld", &x, &y, &a, &b);
    e[x].push_back(y);
    e[y].push_back(x);
    cst[x][y] = cst[y][x] = a;
    mins[x][y] = mins[y][x] = b;
  }
  for (int i = 1; i <= n; ++i)
    scanf("%lld%lld", &c[i], &d[i]);
  dijk(1, s, 0);
  for (int i = 2; i <= n; ++i) {
    ans = 0x7f7f7f7f7f7f7f7f;
    for (int j = 0; j <= ma; ++j)
      ans = min(ans, f[i][j]);
    printf("%lld\\n", ans);
  }
  return 0;
}

void dijk(int x, int y, ll z) {
  p pp;
  memset(f, MAX_INF, sizeof(f));
  q.push({x, y, z});
  f[x][y] = z;
  while (!q.empty()) {
    pp = q.top();
    q.pop();
    if (pp.t == f[pp.u][pp.m])
      update(pp.u, pp.m, pp.t);
  }
}

void update(int u, int m, ll t) {
  int v = u, nm = m;
  ll nt = t;
  for (int i = 1;; ++i) {
    nm += c[u];
    nt += d[u];
    if (nm > ma)
      nm = ma;
    if (nt < f[v][nm]) {
      f[v][nm] = nt;
      q.push({v, nm, nt});
    } else
      break;
    if (nm == ma)
      break;
  }
  for (int i = 0; i < e[u].size(); ++i) {
    v = e[u][i];
    if (m < cst[u][v])
      continue;
    nm = m - cst[u][v];
    nt = t + mins[u][v];
    if (nt < f[v][nm]) {
      f[v][nm] = nt;
      q.push({v, nm, nt});
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x7f
#define MAX_INF_VAL 0x7f7f7f7f
#define pi 3.141592653589
#define eps 1e-10
// #define p 2173412051LL
#define sz 2

using namespace std;

struct p {
  int u, m;
  ll t;
};
struct cmp {
  bool operator()(const p &a, const p &b) {
    if (a.t == b.t)
      return a.m < b.m;
    return a.t > b.t;
  }
};
priority_queue<p, vector<p>, cmp> q;
ll f[51][5050];
vector<int> e[51];
ll cst[51][51], mins[51][51];
ll c[51], d[51];
int ma;

void dijk(int, int, ll);
void update(int, int, ll);

int main() {
  int n, mm, s, x, y;
  ll a, b;
  ll u, m, t;
  ll ans;
  scanf("%d%d%d", &n, &mm, &s);
  ma = mm * 50;
  s = min(s, ma);
  for (int i = 0; i < mm; ++i) {
    scanf("%d%d%lld%lld", &x, &y, &a, &b);
    e[x].push_back(y);
    e[y].push_back(x);
    cst[x][y] = cst[y][x] = a;
    mins[x][y] = mins[y][x] = b;
  }
  for (int i = 1; i <= n; ++i)
    scanf("%lld%lld", &c[i], &d[i]);
  dijk(1, s, 0);
  for (int i = 2; i <= n; ++i) {
    ans = 0x7f7f7f7f7f7f7f7f;
    for (int j = 0; j <= ma; ++j)
      ans = min(ans, f[i][j]);
    printf("%lld\\n", ans);
  }
  return 0;
}

void dijk(int x, int y, ll z) {
  p pp;
  memset(f, MAX_INF, sizeof(f));
  q.push({x, y, z});
  f[x][y] = z;
  while (!q.empty()) {
    pp = q.top();
    q.pop();
    if (pp.t == f[pp.u][pp.m])
      update(pp.u, pp.m, pp.t);
  }
}

void update(int u, int m, ll t) {
  int v = u, nm = m;
  ll nt = t;
  for (int i = 1;; ++i) {
    nm += c[u];
    nt += d[u];
    if (nm > ma)
      nm = ma;
    if (nt < f[v][nm]) {
      f[v][nm] = nt;
      q.push({v, nm, nt});
    }
    if (nm == ma)
      break;
  }
  for (int i = 0; i < e[u].size(); ++i) {
    v = e[u][i];
    if (m < cst[u][v])
      continue;
    nm = m - cst[u][v];
    nt = t + mins[u][v];
    if (nt < f[v][nm]) {
      f[v][nm] = nt;
      q.push({v, nm, nt});
    }
  }
}