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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

struct state {
  int p, m, c;
  state(int p, int m, int c) : p(p), m(m), c(c){};
  bool operator<(const state &o) const { return c < o.c; }
};

int main() {
  int n, m, c;
  while (cin >> n >> m >> c, n) {
    vvi path(n, vi(n, INF));
    int f, t, cc;
    REP(i, m) {
      cin >> f >> t >> cc;
      path[f - 1][t - 1] = min(cc, path[f - 1][t - 1]);
    }

    vvi cost(n, vi(m + 1, INF));
    cost[0][0] = 0;

    priority_queue<state> Q;
    Q.push(state(0, 0, 0));
    int ans = INF;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();
      int cp = st.p, cm = st.m, cc = st.c;

      if (cp == n - 1) {
        if (cc <= c) {
          ans = min(ans, cm);
        }
        continue;
      }

      if (c < cc) {
        continue;
      }

      if (ans < cm) {
        continue;
      }

      REP(i, n) {
        if (i != cp && path[cp][i] != INF) {
          int nc = cc + path[cp][i];
          if (cost[i][cm] > nc) {
            cost[i][cm] = nc;
            Q.push(state(i, cm, nc));
          }
          if (cm + 1 <= m && cost[i][cm + 1] > cc) {
            cost[i][cm + 1] = cc;
            Q.push(state(i, cm + 1, cc));
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

struct state {
  int p, m, c;
  state(int p, int m, int c) : p(p), m(m), c(c){};
  bool operator<(const state &o) const { return c < o.c; }
};

int main() {
  int n, m, c;
  while (cin >> n >> m >> c, n) {
    vvi path(n, vi(n, INF));
    int f, t, cc;
    REP(i, m) {
      cin >> f >> t >> cc;
      path[f - 1][t - 1] = min(cc, path[f - 1][t - 1]);
    }

    vvi cost(n, vi(m + 1, INF));
    cost[0][0] = 0;

    priority_queue<state> Q;
    Q.push(state(0, 0, 0));
    int ans = INF;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();

      if (c <= st.c) {
        continue;
      }

      if (st.p == n - 1) {
        if (st.c <= c) {
          ans = min(ans, st.m);
        }
        continue;
      }

      REP(i, n) {
        if (i != st.p && path[st.p][i] != INF) {
          int nc = st.c + path[st.p][i];
          if (cost[i][st.m] > nc) {
            cost[i][st.m] = nc;
            Q.push(state(i, st.m, nc));
          }
          if (st.m + 1 <= m && cost[i][st.m + 1] > st.c) {
            cost[i][st.m + 1] = st.c;
            Q.push(state(i, st.m + 1, st.c));
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}