Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int from, to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(Y) << X;
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M;
edge G[2010];
int d[2010];
bool negative[2010];

int Bellman_Ford(int s) {
  for (int i = 0; i < N; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  while (true) {
    bool update = false;
    for (int i = 0; i < M; i++) {
      edge e = G[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 (!update) {
      break;
    }
  }
}

void FN() {
  fill(d, d + N, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = G[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == N - 1) {
          negative[e.to] = true;
        }
      }
    }
  }
}

signed main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    G[i].from = a - 1;
    G[i].to = b - 1;
    G[i].cost = -c;
  }
  FN();
  DEB(negative[0]) END if (negative[N - 1] == true) { cout << "inf" << endl; }
  else {
    Bellman_Ford(0);
    cout << -d[N - 1] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
typedef std::pair<int, int> P;
struct edge {
  int from, to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define MAX(X, Y) (*max_element(X, Y))
#define MIN(X, Y) (*min_element(X, Y))
#define NP(X, Y) next_permutation(X, Y)
#define setp(X, Y) cout << fixed << setprecision(Y) << X;
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M;
edge G[2010];
int d[2010];
bool negative[2010];

int Bellman_Ford(int s) {
  for (int i = 0; i < N; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  for (int i = 0; i < N; i++) {
    bool update = false;
    for (int i = 0; i < M; i++) {
      edge e = G[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 (!update) {
      break;
    }
  }
}

void FN() {
  fill(d, d + N, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = G[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == N - 1) {
          negative[e.to] = true;
        }
      }
    }
  }
}

signed main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    G[i].from = a - 1;
    G[i].to = b - 1;
    G[i].cost = -c;
  }
  FN();
  DEB(negative[0]) END if (negative[N - 1] == true) { cout << "inf" << endl; }
  else {
    Bellman_Ford(0);
    cout << -d[N - 1] << endl;
  }
  return 0;
}