Problem p00890 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
ll const mod = 1000000007;
int const inf = 1LL << 29;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src_, int dst_, Weight weight_)
      : src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int n, m, c;
Graph g;
int dp[110][110];

int solve() {
  int ans = inf;
  // 距離、今いる頂点、0にした回数
  // 距離がcより小さくなったら
  typedef tuple<int, int, int> state;
  priority_queue<state> q;
  rep(i, 101) rep(j, 101) dp[i][j] = inf;
  q.emplace(0, 0, 0);
  dp[0][0] = 0;
  while (q.size()) {
    int d, v, r;
    tie(d, v, r) = q.top();
    q.pop();
    d *= -1;
    if (dp[v][r] < d)
      continue;
    if (r > n)
      continue;
    if (d > c)
      continue;
    if (v == n - 1)
      ans = min(ans, r);
    for (auto &e : g[v]) {
      if (dp[e.dst][r] > d + e.weight) {
        dp[e.dst][r] = d + e.weight;
        q.emplace(-(d + e.weight), e.dst, r);
      }
      if (dp[e.dst][r + 1] > d) {
        dp[e.dst][r + 1] = d;
        q.emplace(-d, e.dst, r + 1);
      }
    }
  }
  return ans;
}

int main() {
#ifdef LOCAL
  cout << "TEST" << endl;
  freopen("in", "r", stdin);
#endif // LOCAL

  while (cin >> n >> m >> c && n) {
    g.assign(n, {});
    rep(i, m) {
      int f, t, c;
      cin >> f >> t >> c;
      f--, t--;
      g[f].eb(f, t, c);
    }
    cout << solve() << endl;
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
ll const mod = 1000000007;
int const inf = 1LL << 29;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src_, int dst_, Weight weight_)
      : src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int n, m, c;
Graph g;
bool vis[101][101];

int solve(Graph &g) {
  int ans = inf;
  // 距離、今いる頂点、0にした回数
  // 距離がcより小さくなったら
  typedef tuple<int, int, int> state;
  priority_queue<state> q;
  rep(i, 101) rep(j, 101) vis[i][j] = false;
  q.emplace(0, 0, 0);
  while (q.size()) {
    int d, v, r;
    tie(d, v, r) = q.top();
    d *= -1;
    q.pop();
    if (vis[v][r])
      continue;
    vis[0][0] = true;
    if (r > n)
      continue;
    if (d > c)
      continue;
    if (v == n - 1)
      ans = min(ans, r);
    for (auto &e : g[v]) {
      q.emplace(-(d + e.weight), e.dst, r);
      q.emplace(-d, e.dst, r + 1);
    }
  }
  return ans;
}

int main() {
#ifdef LOCAL
  cout << "TEST" << endl;
  freopen("in", "r", stdin);
#endif // LOCAL

  while (cin >> n >> m >> c && n) {
    g.assign(n, {});
    rep(i, m) {
      int f, t, c;
      cin >> f >> t >> c;
      f--, t--;
      g[f].eb(f, t, c);
    }
    cout << solve(g) << endl;
  }
}