Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int INF = 1 << 29;
ll inf = 1LL << 50;

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

edge es[2010]; // MAX_E
ll d[1010];    // S→Gのcost //MAX_V
ll N, M;

void bellman_ford(ll s) {
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int 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 (!update)
      break;
  }
}

bool find_negative_roop(ll s) {
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[s] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == N - 1)
          return true;
      }
    }
  }
  return false;
}
vector<bool> nega(N);
void find_negative_roop2(ll s) {
  for (int i = 0; i < N; i++)
    nega[i] = false;
  // d[s]=0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        nega[e.to] = true;
        // if(i==N-1) return true;
      }
    }
  }
  // return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    z = -z;
    es[i].from = x;
    es[i].to = y;
    es[i].cost = z;
  }
  find_negative_roop(0);
  find_negative_roop2(0);
  if (nega[N - 1]) {
    cout << "inf" << endl;
  } else {
    cout << -d[N - 1] << endl;
  }
  /*if(find_negative_roop(0)){
          cout<<"inf"<<endl;
  }else{
          cout<<-d[N-1]<<endl;
  }*/
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int INF = 1 << 29;
ll inf = 1LL << 50;

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

edge es[2010]; // MAX_E
ll d[1010];    // S→Gのcost //MAX_V
ll N, M;

void bellman_ford(ll s) {
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int 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 (!update)
      break;
  }
}

bool find_negative_roop(ll s) {
  for (int i = 0; i < N; i++)
    d[i] = inf;
  d[s] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == N - 1)
          return true;
      }
    }
  }
  return false;
}
vector<bool> nega(1010);
void find_negative_roop2(ll s) {
  for (int i = 0; i < N; i++)
    nega[i] = false;
  // d[s]=0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        nega[e.to] = true;
        // if(i==N-1) return true;
      }
    }
  }
  // return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    z = -z;
    es[i].from = x;
    es[i].to = y;
    es[i].cost = z;
  }
  find_negative_roop(0);
  find_negative_roop2(0);
  if (nega[N - 1]) {
    cout << "inf" << endl;
  } else {
    cout << -d[N - 1] << endl;
  }
  /*if(find_negative_roop(0)){
          cout<<"inf"<<endl;
  }else{
          cout<<-d[N-1]<<endl;
  }*/
  return 0;
}