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

#include <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 55, maxsreq = 20005;

int n, m, s, u, v, a, b, c[maxn], d[maxn], mintime[maxn][maxsreq];
vector<pair<int, pii>> adj[maxn];

void dijkstra(int silverreq) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= silverreq; j++)
      mintime[i][j] = (int)4e18;

  mintime[0][s] = 0;
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.push({0, {0, s}});
  while (!pq.empty()) {
    int t = pq.top().first;
    pii state = pq.top().second;
    pq.pop();
    if (t != mintime[state.first][state.second])
      continue;
    for (auto v : adj[state.first])
      if (state.second >= v.second.first &&
          mintime[v.first][state.second - v.second.first] >
              t + v.second.second) {
        mintime[v.first][state.second - v.second.first] = t + v.second.second;
        pq.push({mintime[v.first][state.second - v.second.first],
                 {v.first, state.second - v.second.first}});
      }
    for (int j = 1; state.second + c[state.first] * (j - 1) < silverreq; j++) {
      int newsum = state.second + c[state.first] * j;
      if (newsum > silverreq)
        newsum = silverreq;
      if (mintime[state.first][newsum] > t + j * d[state.first]) {
        mintime[state.first][newsum] = t + j * d[state.first];
        pq.push({mintime[state.first][newsum], {state.first, newsum}});
      }
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  int sumtripcosts = 0;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    sumtripcosts += a;
    u--;
    v--;
    adj[u].push_back({v, {a, b}});
    adj[v].push_back({u, {a, b}});
  }
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  s = min(s, sumtripcosts);
  dijkstra(sumtripcosts);
  for (int i = 1; i < n; i++) {
    int curans = (int)4e18;
    for (int j = 0; j <= sumtripcosts; j++)
      curans = min(curans, mintime[i][j]);
    cout << curans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
const int MOD = 1e9 + 7;
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 55, maxsreq = 40005;

int n, m, s, u, v, a, b, c[maxn], d[maxn], mintime[maxn][maxsreq];
vector<pair<int, pii>> adj[maxn];

void dijkstra(int silverreq) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= silverreq; j++)
      mintime[i][j] = (int)4e18;

  mintime[0][s] = 0;
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.push({0, {0, s}});
  while (!pq.empty()) {
    int t = pq.top().first;
    pii state = pq.top().second;
    pq.pop();
    if (t != mintime[state.first][state.second])
      continue;
    for (auto v : adj[state.first])
      if (state.second >= v.second.first &&
          mintime[v.first][state.second - v.second.first] >
              t + v.second.second) {
        mintime[v.first][state.second - v.second.first] = t + v.second.second;
        pq.push({mintime[v.first][state.second - v.second.first],
                 {v.first, state.second - v.second.first}});
      }
    for (int j = 1; state.second + c[state.first] * (j - 1) < silverreq; j++) {
      int newsum = state.second + c[state.first] * j;
      if (newsum > silverreq)
        newsum = silverreq;
      if (mintime[state.first][newsum] > t + j * d[state.first]) {
        mintime[state.first][newsum] = t + j * d[state.first];
        pq.push({mintime[state.first][newsum], {state.first, newsum}});
      }
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> s;
  int sumtripcosts = 0;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    sumtripcosts += a;
    u--;
    v--;
    adj[u].push_back({v, {a, b}});
    adj[v].push_back({u, {a, b}});
  }
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  s = min(s, 2 * sumtripcosts);
  dijkstra(2 * sumtripcosts);
  for (int i = 1; i < n; i++) {
    int curans = (int)4e18;
    for (int j = 0; j <= 2 * sumtripcosts; j++)
      curans = min(curans, mintime[i][j]);
    cout << curans << "\\n";
  }
  return 0;
}