Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
#define MV 1000
struct edge {
  int to, cost;
};
vector<edge> G[MV];
int V, E;
ll d[MV + 1];
bool bellmanford(int s) {
  rep(i, V) { d[i] = INFl; }
  d[s] = 0;
  int c = 0;
  // bool upd = false;
  bool goalupd = false; // 出発点から目的地までの経路での負の閉路の存在判定
  while (true) {
    // upd = false;
    goalupd = false;
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        edge e = G[i][j];
        if (d[i] != INFl && d[e.to] > d[i] + e.cost) {
          // printf("c:%d i:%d to:%d oldcost:%lld
          // newcost:%lld\n",c,i,e.to,d[e.to],d[i] + e.cost);
          d[e.to] = d[i] + e.cost;
          // upd = true;
          if (e.to == V - 1)
            goalupd = true;
        }
      }
    }
    if (c >= V - 1 && goalupd)
      return true;
    if (c == 2 * V - 1)
      return false;
    c++;
  }
}
int main() {
  int P, A[5001], B[5001], C[5001];
  scan(V >> E >> P);
  rep(i, E) {
    scan(A[i] >> B[i] >> C[i]);
    edge e;
    e.to = B[i] - 1;
    e.cost = -C[i] + P;
    G[A[i] - 1].push_back(e);
  }
  bool flag = bellmanford(0);
  if (flag)
    prin(-1);
  else {
    prin(max(0LL, -d[V - 1]));
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
#define MV 5000
struct edge {
  int to, cost;
};
vector<edge> G[MV];
int V, E;
ll d[MV + 1];
bool bellmanford(int s) {
  rep(i, V) { d[i] = INFl; }
  d[s] = 0;
  int c = 0;
  // bool upd = false;
  bool goalupd = false; // 出発点から目的地までの経路での負の閉路の存在判定
  while (true) {
    // upd = false;
    goalupd = false;
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        edge e = G[i][j];
        if (d[i] != INFl && d[e.to] > d[i] + e.cost) {
          // printf("c:%d i:%d to:%d oldcost:%lld
          // newcost:%lld\n",c,i,e.to,d[e.to],d[i] + e.cost);
          d[e.to] = d[i] + e.cost;
          // upd = true;
          if (e.to == V - 1)
            goalupd = true;
        }
      }
    }
    if (c >= V - 1 && goalupd)
      return true;
    if (c == 2 * V - 1)
      return false;
    c++;
  }
}
int main() {
  int P, A[5001], B[5001], C[5001];
  scan(V >> E >> P);
  rep(i, E) {
    scan(A[i] >> B[i] >> C[i]);
    edge e;
    e.to = B[i] - 1;
    e.cost = -C[i] + P;
    G[A[i] - 1].push_back(e);
  }
  bool flag = bellmanford(0);
  if (flag)
    prin(-1);
  else {
    prin(max(0LL, -d[V - 1]));
  }
  return 0;
}