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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const ll N = 52;
const ll M = 102;
const ll inf = 7e18;

struct nes {
  ll v, a, b;
};

ll n, m, s;
vector<nes> g[N];
ll c[N], d[N];
ll dist[N][50 * N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }

  s = min(s, (n - 1) * 50ll);

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 50 * (n - 1); j++) {
      dist[i][j] = inf;
    }
  }
  /*priority_*/ queue<pair<ll, pair<ll, ll>>> pq;
  pq.push({0, {1, s}});
  dist[1][s] = 0;
  while (!pq.empty()) {
    auto sta = pq.front();
    pq.pop();
    ll raz = sta.first, u = sta.second.first, sil = sta.second.second;
    for (int j = sil + c[u]; j < (n - 1) * 50; j += c[u]) {
      ll dod = ((j - sil) / c[u]) * d[u];
      if (dist[u][j] > dist[u][sil] + dod) {
        dist[u][j] = dist[u][sil] + dod;
        pq.push({-dist[u][j], {u, j}});
      }
    }
    for (auto f : g[u]) {
      ll v = f.v, a = f.a, b = f.b;
      if (sil >= a && dist[v][sil - a] > dist[u][sil] + b) {
        dist[v][sil - a] = dist[u][sil] + b;
        pq.push({-dist[v][sil - a], {v, sil - a}});
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    ll sol = inf;
    for (int j = 0; j <= 50 * (n - 1); j++) {
      sol = min(sol, dist[i][j]);
    }
    cout << sol << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const ll N = 52;
const ll M = 102;
const ll inf = 7e18;

struct nes {
  ll v, a, b;
};

ll n, m, s;
vector<nes> g[N];
ll c[N], d[N];
ll dist[N][50 * N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].push_back({v, a, b});
    g[v].push_back({u, a, b});
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }

  s = min(s, (n - 1) * 50ll);

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 50 * (n - 1); j++) {
      dist[i][j] = inf;
    }
  }
  priority_queue<pair<ll, pair<ll, ll>>> pq;
  pq.push({0, {1, s}});
  dist[1][s] = 0;
  while (!pq.empty()) {
    auto sta = pq.top();
    pq.pop();
    ll raz = sta.first, u = sta.second.first, sil = sta.second.second;
    for (int j = sil + c[u]; j < (n - 1) * 50; j += c[u]) {
      ll dod = ((j - sil) / c[u]) * d[u];
      if (dist[u][j] > dist[u][sil] + dod) {
        dist[u][j] = dist[u][sil] + dod;
        pq.push({-dist[u][j], {u, j}});
      }
    }
    for (auto f : g[u]) {
      ll v = f.v, a = f.a, b = f.b;
      if (sil >= a && dist[v][sil - a] > dist[u][sil] + b) {
        dist[v][sil - a] = dist[u][sil] + b;
        pq.push({-dist[v][sil - a], {v, sil - a}});
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    ll sol = inf;
    for (int j = 0; j <= 50 * (n - 1); j++) {
      sol = min(sol, dist[i][j]);
    }
    cout << sol << endl;
  }
}