Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define NMAX 1005
#define MMAX 2005
#define INF (long long)1e18

// コメントアウトするとdebug()を実行しない
#define DEBUG

using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef pair<ll, ll> P;

struct edge {
  ll from, to, cost;
};

ll N, M, ans, d[NMAX];
edge es[NMAX];
string S;
static const ll dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
                dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

bool bellman_ford() {
  Fill(d, -INF);
  d[1] = 0;
  bool update = false;
  ll num = 0, nat;
  do {
    update = false;
    for (ll i = 0; i < M; ++i) {
      edge e = es[i];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (num == N - 2) {
      nat = d[N];
    }
    if (num > 2 * N && update) {
      if (nat != d[N]) {
        return true;
      }
      return false;
    }
    num++;
  } while (update);
  return false;
}

void solve() {
  if (bellman_ford()) {
    ans = INF;
  }
}

void debug() {}

void answer() {
  if (ans == INF) {
    cout << "inf\n";
  } else {
    cout << d[N] << "\n";
  }
}

int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; ++i) {
    ll A, B, C;
    cin >> A >> B >> C;
    es[i] = (edge){A, B, C};
  }
  solve();
#ifdef DEBUG
  debug();
#endif
  answer();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define NMAX 1005
#define MMAX 2005
#define INF (long long)1e18

// コメントアウトするとdebug()を実行しない
#define DEBUG

using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef pair<ll, ll> P;

struct edge {
  ll from, to, cost;
};

ll N, M, ans, d[NMAX];
edge es[MMAX];
string S;
static const ll dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
                dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

bool bellman_ford() {
  Fill(d, -INF);
  d[1] = 0;
  bool update = false;
  ll num = 0, nat;
  do {
    update = false;
    for (ll i = 0; i < M; ++i) {
      edge e = es[i];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (num == N - 2) {
      nat = d[N];
    }
    if (num > 2 * N && update) {
      if (nat != d[N]) {
        return true;
      }
      return false;
    }
    num++;
  } while (update);
  return false;
}

void solve() {
  if (bellman_ford()) {
    ans = INF;
  }
}

void debug() {}

void answer() {
  if (ans == INF) {
    cout << "inf\n";
  } else {
    cout << d[N] << "\n";
  }
}

int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; ++i) {
    ll A, B, C;
    cin >> A >> B >> C;
    es[i] = (edge){A, B, C};
  }
  solve();
#ifdef DEBUG
  debug();
#endif
  answer();

  return 0;
}