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

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 52;

int n, m, s;

vector<int> dist[N];
vector<bool> vis[N];

vector<vector<int>> convert;
vector<vector<int>> v[N];

int32_t main() {
  _

          cin >>
      n >> m >> s;
  const int M = 50 * n;

  for (int i = 0; i < N; ++i) {
    dist[i].resize(M + 10, inf);
    vis[i].resize(M + 10, 0);
  }

  for (int i = 0; i < m; ++i) {
    int x, y, silver, time;
    cin >> x >> y >> silver >> time;
    v[x].push_back({y, silver, time});
    v[y].push_back({x, silver, time});
  }

  convert.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    int c, d;
    cin >> c >> d;
    convert[i] = {c, d};
  }

  s = min(s, M);
  dist[1][s] = 0;

  set<vector<int>> ms;  // function of time
  ms.insert({0, s, 1}); // inserted time, silver coins, vertex

  vector<int> ans(n + 1, inf);
  while (!ms.empty()) {
    vector<int> f = *ms.begin();
    ms.erase(ms.begin());

    int time = f[0], vertex = f[2], silvers = f[1];

    if (vis[vertex][silvers]) {
      continue;
    }

    tr(f); // 2500*50
    vis[vertex][silvers] = 1;
    smin(ans[vertex], time);
    for (int i = silvers + convert[vertex][0], ch = time + convert[vertex][1];
         i <= M; i += convert[vertex][0], ch += convert[vertex][1]) {
      if (dist[vertex][i] > ch) {
        dist[vertex][i] = ch;
        ms.insert({ch, i, vertex});
      }
    }

    for (auto it : v[vertex]) {
      int nxt_vertex = it[0];
      int required_silver = it[1];
      int update_time = it[2];
      if (silvers >= required_silver) {
        if (dist[nxt_vertex][silvers - required_silver] >
            dist[vertex][silvers] + update_time) {
          dist[nxt_vertex][silvers - required_silver] =
              dist[vertex][silvers] + update_time;
          ms.insert({dist[vertex][silvers] + update_time,
                     silvers - required_silver, nxt_vertex});
        }
      }
    }
  }

  for (int i = 2; i <= n; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 52;
const int M = 2450;

int n, m, s;
int dist[N][M + 10];
bool vis[N][M + 10];

vector<vector<int>> convert;
vector<vector<int>> v[N];

int32_t main() {
  _

          cin >>
      n >> m >> s;

  for (int i = 0; i < m; ++i) {
    int x, y, silver, time;
    cin >> x >> y >> silver >> time;
    v[x].push_back({y, silver, time});
    v[y].push_back({x, silver, time});
  }

  convert.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    int c, d;
    cin >> c >> d;
    convert[i] = {c, d};
  }

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= M; ++j) {
      dist[i][j] = inf;
    }
  }

  s = min(s, M);
  dist[1][s] = 0;

  set<vector<int>> ms;  // function of time
  ms.insert({0, s, 1}); // inserted time, silver coins, vertex

  vector<int> ans(n + 1, inf);
  while (!ms.empty()) {
    vector<int> f = *ms.begin();
    ms.erase(ms.begin());

    int time = f[0], vertex = f[2], silvers = f[1];

    if (vis[vertex][silvers]) {
      continue;
    }

    tr(f); // 2500*50
    vis[vertex][silvers] = 1;
    smin(ans[vertex], time);
    for (int i = silvers + convert[vertex][0], ch = time + convert[vertex][1];
         i <= M; i += convert[vertex][0], ch += convert[vertex][1]) {
      if (dist[vertex][i] > ch) {
        dist[vertex][i] = ch;
        ms.insert({ch, i, vertex});
      }
    }

    for (auto it : v[vertex]) {
      int nxt_vertex = it[0];
      int required_silver = it[1];
      int update_time = it[2];
      if (silvers >= required_silver) {
        if (dist[nxt_vertex][silvers - required_silver] >
            dist[vertex][silvers] + update_time) {
          dist[nxt_vertex][silvers - required_silver] =
              dist[vertex][silvers] + update_time;
          ms.insert({dist[vertex][silvers] + update_time,
                     silvers - required_silver, nxt_vertex});
        }
      }
    }
  }

  for (int i = 2; i <= n; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}