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

#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair<int, int>
#define iii tuple<int, int, int>
#define int long long
using namespace std;

const int INF = 1e18;
int n, m, s;
int cost[55][55], tim[55][55];
int silver[55], lost[55];
vector<int> ed[200005];
int ans[55][5505];
bool done[55][5505];
int lim;

void bfs() {
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 5005; j++)
      ans[i][j] = INF;

  ans[1][s] = 0;

  priority_queue<iii, vector<iii>, greater<iii>> q;
  q.push(iii(0, 1, s));

  while (!q.empty()) {
    int u = get<1>(q.top());
    int cur = get<2>(q.top());
    q.pop();
    int save = cur;

    if (done[u][cur])
      continue;
    done[u][cur] = 1;

    for (auto v : ed[u]) {
      int cnt = 0;
      while (cur - cost[u][v] <= lim) {
        if (cur - cost[u][v] >= 0 &&
            ans[v][cur - cost[u][v]] >
                ans[u][save] + cnt * lost[u] + tim[u][v]) {
          ans[v][cur - cost[u][v]] = ans[u][save] + cnt * lost[u] + tim[u][v];
          q.push(iii(ans[v][cur - cost[u][v]], v, cur - cost[u][v]));
        }

        cnt++;
        cur += silver[u];
      }
      cur = save;
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen("depart.inp", "r", stdin);
  //    freopen("depart.out", "w", stdout);

  cin >> n >> m >> s;
  for (int i = 1; i <= m; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    c = min(5000LL, c);
    lim = max(lim, c);
    cost[a][b] = c;
    cost[b][a] = c;
    tim[b][a] = t;
    tim[a][b] = t;
    ed[a].push_back(b);
    ed[b].push_back(a);
  }

  lim *= m;
  s = min(s, lim);
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    silver[i] = a;
    lost[i] = b;
  }

  bfs();

  for (int i = 2; i <= n; i++) {
    int res = INF;
    for (int j = 0; j <= 5000; j++)
      res = min(res, ans[i][j]);
    cout << res << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair<int, int>
#define iii tuple<int, int, int>
#define int long long
using namespace std;

const int INF = 1e18;
int n, m, s;
int cost[55][55], tim[55][55];
int silver[55], lost[55];
vector<int> ed[200005];
int ans[55][5505];
bool done[55][5505];

void bfs() {
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 5000; j++)
      ans[i][j] = INF;

  ans[1][s] = 0;

  priority_queue<iii, vector<iii>, greater<iii>> q;
  q.push(iii(0, 1, s));

  while (!q.empty()) {
    int u = get<1>(q.top());
    int cur = get<2>(q.top());
    q.pop();
    int save = cur;

    if (done[u][cur])
      continue;
    done[u][cur] = 1;

    for (auto v : ed[u]) {
      int cnt = 0;
      while (cur - cost[u][v] <= 5000) {
        //                cerr << u << ' ' << u << " " << cur << "\\n";

        if (cur - cost[u][v] >= 0 &&
            ans[v][cur - cost[u][v]] >
                ans[u][save] + cnt * lost[u] + tim[u][v]) {
          ans[v][cur - cost[u][v]] = ans[u][save] + cnt * lost[u] + tim[u][v];
          q.push(iii(ans[v][cur - cost[u][v]], v, cur - cost[u][v]));
        }

        cnt++;
        cur += silver[u];
      }
      cur = save;
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen("depart.inp", "r", stdin);
  //    freopen("depart.out", "w", stdout);

  cin >> n >> m >> s;
  s = min(s, 5000LL);
  for (int i = 1; i <= m; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    cost[a][b] = c;
    cost[b][a] = c;
    tim[b][a] = t;
    tim[a][b] = t;
    ed[a].push_back(b);
    ed[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    silver[i] = a;
    lost[i] = b;
  }
  bfs();
  for (int i = 2; i <= n; i++) {
    int res = INF;
    for (int j = 0; j <= 5000; j++)
      res = min(res, ans[i][j]);
    cout << res << "\\n";
  }
}