Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef long long int llint;
typedef pair<ll, ll> pa;
#define MM 1000000000
#define MOD MM + 7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define MAX_T 1001
#define pi pair<int, int>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF (1 << 29) // 536870912

const long double PI = acos(-1);
const ll DEP = 1LL << 50;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int GCD(int x, int y) { return y ? GCD(y, x % y) : x; }
const int NMAX = 1000;
const int MMAX = 2000;
int N, M;
int a[MMAX], b[MMAX];
ll c[MMAX];
int main() {
  cin >> N >> M;
  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] = DEP;
  }
  dist[0] = 0;

  for (int loop = 0; loop = N - 1; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i] - 1] == DEP)
        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_edge[NMAX];

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

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i] - 1] == DEP)
        continue;
      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative_edge[b[i] - 1] = true;
      }

      if (negative_edge[a[i] - 1] == true) {
        negative_edge[b[i] - 1] = true;
      }
    }
  }
  if (negative_edge[N - 1]) {
    cout << "inf" << endl;
  } else {
    cout << -ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef long long int llint;
typedef pair<ll, ll> pa;
#define MM 1000000000
#define MOD MM + 7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define MAX_T 1001
#define pi pair<int, int>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF (1 << 29) // 536870912

const long double PI = acos(-1);
const ll DEP = 1LL << 50;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int GCD(int x, int y) { return y ? GCD(y, x % y) : x; }
const int NMAX = 1000;
const int MMAX = 2000;
int N, M;
int a[MMAX], b[MMAX];
ll c[MMAX];
int main() {
  cin >> N >> M;
  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] = DEP;
  }
  dist[0] = 0;

  for (int loop = 0; loop < N - 1; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i] - 1] == DEP)
        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_edge[NMAX];

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

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i] - 1] == DEP)
        continue;
      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative_edge[b[i] - 1] = true;
      }

      if (negative_edge[a[i] - 1] == true) {
        negative_edge[b[i] - 1] = true;
      }
    }
  }
  if (negative_edge[N - 1]) {
    cout << "inf" << endl;
  } else {
    cout << -ans << endl;
  }
}