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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int maxS = 2600;

ll d[51][maxS];
bool used[51][maxS];

struct Edge {
  int to, cost, time;
};

vector<Edge> g[51];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, s;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    g[a].push_back({b, c, d});
    g[b].push_back({a, c, d});
  }
  vector<int> co(n + 1), ti(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> co[i] >> ti[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < maxS; j++) {
      d[i][j] = 1e18;
    }
  }
  d[1][min(s, maxS - 1)] = 0;
  set<pair<int, pair<int, int>>> setV;
  setV.insert({0, {1, min(s, maxS - 1)}});
  while (!setV.empty()) {
    auto p = setV.begin()->second;
    int x = p.first;
    int sum = p.second;
    setV.erase(setV.begin());
    used[x][sum] = true;
    for (auto e : g[x]) {
      if (sum >= e.cost && d[e.to][sum - e.cost] > d[x][sum] + e.time) {
        setV.erase({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
        d[e.to][sum - e.cost] = d[x][sum] + e.time;
        setV.insert({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
      }
    }
    if (d[x][min(maxS - 1, sum + co[x])] > d[x][sum] + ti[x]) {
      setV.erase(
          {d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
      d[x][min(maxS - 1, sum + co[x])] = d[x][sum] + ti[x];
      setV.insert(
          {d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
    }
  }
  for (int i = 2; i <= n; i++) {
    ll ans = 1e18;
    for (int j = 0; j < maxS; j++) {
      ans = min(ans, d[i][j]);
    }
    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int maxS = 5000;

ll d[51][maxS];
bool used[51][maxS];

struct Edge {
  int to, cost, time;
};

vector<Edge> g[51];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, s;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    g[a].push_back({b, c, d});
    g[b].push_back({a, c, d});
  }
  vector<int> co(n + 1), ti(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> co[i] >> ti[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < maxS; j++) {
      d[i][j] = 1e18;
    }
  }
  d[1][min(s, maxS - 1)] = 0;
  set<pair<int, pair<int, int>>> setV;
  setV.insert({0, {1, min(s, maxS - 1)}});
  while (!setV.empty()) {
    auto p = setV.begin()->second;
    int x = p.first;
    int sum = p.second;
    setV.erase(setV.begin());
    used[x][sum] = true;
    for (auto e : g[x]) {
      if (sum >= e.cost && d[e.to][sum - e.cost] > d[x][sum] + e.time) {
        setV.erase({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
        d[e.to][sum - e.cost] = d[x][sum] + e.time;
        setV.insert({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
      }
    }
    if (d[x][min(maxS - 1, sum + co[x])] > d[x][sum] + ti[x]) {
      setV.erase(
          {d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
      d[x][min(maxS - 1, sum + co[x])] = d[x][sum] + ti[x];
      setV.insert(
          {d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
    }
  }
  for (int i = 2; i <= n; i++) {
    ll ans = 1e18;
    for (int j = 0; j < maxS; j++) {
      ans = min(ans, d[i][j]);
    }
    cout << ans << "\\n";
  }
}