Problem p03722 (C++)

Buggy submission (status: Runtime Error):

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#endif

using ll = long long;
const ll INF = 1LL << 50;

int main(void) {
  using std::cin;
  using std::cout;
  using std::endl;
  int N, M;
  cin >> N >> M;

  const int NMAX = 1000;
  const int MMAX = 2000;
  int a[MMAX], b[MMAX];
  ll c[MMAX];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    --a[i];
    --b[i];
    c[i] = -c[i];
  }

  ll dist[NMAX];

  for (int i = 0; i < N; ++i) {
    dist[i] = INF;
  }

  dist[0] = 0;

  for (int loop = 0; loop < N - 1; ++loop) {
    for (int i = 0; i < M; ++i) {

      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
      }
    }
  }

  ll ans = dist[N - 1];

  bool negative[NMAX];

  for (int i = 0; i < N; ++i) {
    negative[i] = false;
  }

  for (int loop = 0; loop < N; ++loop) {
    for (int i = 0; i < M; ++i) {
      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative[b[i] - 1] = true;
      }

      if (negative[a[i] - 1] == true) {
        negative[b[i] - 1] = true;
      }
    }
  }

  if (negative[N - 1])
    cout << "inf" << endl;
  else
    cout << -ans << endl;

  return 0;
}

Fixed submission:

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#endif

using ll = long long;
const ll INF = 1LL << 50;

int main(void) {
  using std::cin;
  using std::cout;
  using std::endl;
  int N, M;
  cin >> N >> M;

  const int NMAX = 1000;
  const int MMAX = 2000;
  int a[MMAX], b[MMAX];
  ll c[MMAX];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] = -c[i];
  }

  ll dist[NMAX];

  for (int i = 0; i < N; ++i) {
    dist[i] = INF;
  }

  dist[0] = 0;

  for (int loop = 0; loop < N - 1; ++loop) {
    for (int i = 0; i < M; ++i) {

      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
      }
    }
  }

  ll ans = dist[N - 1];

  bool negative[NMAX];

  for (int i = 0; i < N; ++i) {
    negative[i] = false;
  }

  for (int loop = 0; loop < N; ++loop) {
    for (int i = 0; i < M; ++i) {
      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative[b[i] - 1] = true;
      }

      if (negative[a[i] - 1] == true) {
        negative[b[i] - 1] = true;
      }
    }
  }

  if (negative[N - 1])
    cout << "inf" << endl;
  else
    cout << -ans << endl;

  return 0;
}