Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define INF 9223372036854775807
#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define RFOR(i, a, b) for (long long i = (b - 1); i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<P> Graph;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  int a[n], b[n], c[n];
  REP(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] *= -1; // 最短経路問題に
  }

  ll score[n];
  REP(i, n) score[i] = INF;

  score[0] = 0;

  // BellmanFord法、負閉路がないと仮定してチェック
  REP(i, n - 1) {
    REP(j, m) {
      if (score[a[j]] == INF)
        continue;
      if (score[b[j]] > score[a[j]] + c[j])
        score[b[j]] = score[a[j]] + c[j];
    }
  }

  // 負閉路チェック
  // この時点で負閉路が無い場合は，最適解が得られているため更新はおきない.
  // 負閉路の最大の長さn回分ループして，更新された場所と，そこを経由する部分は負閉路を含む
  bool f[n];
  REP(i, n) f[i] = false;

  REP(i, n) {
    REP(j, m) {
      if (score[a[j]] == INF)
        continue;
      if (score[b[j]] > score[a[j]] + c[j]) {
        score[b[j]] = score[a[j]] + c[j];
        f[b[j]] = true;
      }
      if (f[a[j]])
        f[b[j]] = true;
    }
  }

  if (f[n - 1])
    cout << "inf" << endl;
  else
    cout << -score[n - 1] << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define INF 9223372036854775807
#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define RFOR(i, a, b) for (long long i = (b - 1); i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<P> Graph;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  int a[m], b[m], c[m];
  REP(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] *= -1; // 最短経路問題に
  }

  ll score[n];
  REP(i, n) score[i] = INF;

  score[0] = 0;

  // BellmanFord法、負閉路がないと仮定してチェック
  REP(i, n - 1) {
    REP(j, m) {
      if (score[a[j]] == INF)
        continue;
      if (score[b[j]] > score[a[j]] + c[j])
        score[b[j]] = score[a[j]] + c[j];
    }
  }

  // 負閉路チェック
  // この時点で負閉路が無い場合は，最適解が得られているため更新はおきない.
  // 負閉路の最大の長さn回分ループして，更新された場所と，そこを経由する部分は負閉路を含む
  bool f[n];
  REP(i, n) f[i] = false;

  REP(i, n) {
    REP(j, m) {
      if (score[a[j]] == INF)
        continue;
      if (score[b[j]] > score[a[j]] + c[j]) {
        score[b[j]] = score[a[j]] + c[j];
        f[b[j]] = true;
      }
      if (f[a[j]])
        f[b[j]] = true;
    }
  }

  if (f[n - 1])
    cout << "inf" << endl;
  else
    cout << -score[n - 1] << endl;

  return 0;
}