Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

struct edge {
  int from, to;
  LL cost;
  edge(int from, int to, LL cost) : from(from), to(to), cost(cost) {}
};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr LL INF = 1LL << 50;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<edge> es;
  int a, b, c;
  REP(i, 0, m) {
    cin >> a >> b >> c;
    es.pb(a - 1, b - 1, -c);
  }
  VLL d(n, INF);
  VI prev(n, -1);
  d[0] = 0;
  REP(i, 0, n) {
    REP(j, 0, m) {
      int u = es[j].from;
      int v = es[j].to;
      LL cost = es[j].cost;
      if (d[v] > d[u] + cost) {
        d[v] = d[u] + cost;
        prev[v] = u;
      }
    }
  }

  vector<bool> visited(n, false);
  visited[n - 1] = true;
  int p = prev[n - 1];
  for (;;) {
    if (visited[p]) {
      cout << "inf" << endl;
      return;
    }
    p = prev[p];
    if (p < 0)
      break;
  }

  REP(j, 0, m) {
    int u = es[j].from;
    int v = es[j].to;
    LL cost = es[j].cost;
    if (!visited[u])
      continue;
    if (d[v] > d[u] + cost) {
      cout << "inf" << endl;
      return;
    }
  }

  cout << -d[n - 1] << endl;
}

int main() {
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

struct edge {
  int from, to;
  LL cost;
  edge(int from, int to, LL cost) : from(from), to(to), cost(cost) {}
};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr LL INF = 1LL << 50;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<edge> es;
  int a, b, c;
  REP(i, 0, m) {
    cin >> a >> b >> c;
    es.pb(a - 1, b - 1, -c);
  }
  VLL d(n, INF);
  VI prev(n, -1);
  d[0] = 0;
  REP(i, 0, n) {
    REP(j, 0, m) {
      int u = es[j].from;
      int v = es[j].to;
      LL cost = es[j].cost;
      if (d[v] > d[u] + cost) {
        d[v] = d[u] + cost;
        prev[v] = u;
      }
    }
  }

  vector<bool> visited(n, false);
  visited[n - 1] = true;
  int p = prev[n - 1];
  for (;;) {
    if (visited[p]) {
      cout << "inf" << endl;
      return;
    }
    visited[p] = true;
    p = prev[p];
    if (p < 0)
      break;
  }

  REP(j, 0, m) {
    int u = es[j].from;
    int v = es[j].to;
    LL cost = es[j].cost;
    if (!visited[u])
      continue;
    if (d[v] > d[u] + cost) {
      cout << "inf" << endl;
      return;
    }
  }

  cout << -d[n - 1] << endl;
}

int main() {
  solve();

  return 0;
}