Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 998244353;

struct edge {
  int from;
  int to;
  int cost;
};
int n, m;
int d[2510];

signed main() {
  vector<edge> es;

  int n, m, p;
  cin >> n >> m >> p;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e1 = {a - 1, b - 1, c - p};
    es.push_back(e1);
  }

  fill(d, d + n, -INF);
  d[0] = 0;
  rep(i, n) {
    rep(j, m) {
      struct edge e = es[j];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
      }
    }
  }
  int neg[1010] = {0};
  rep(i, n) {
    rep(j, m) {
      struct edge e = es[j];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        neg[e.to] = 1;
      }
    }
  }
  // rep(i,n) cerr << d[i] << " ";
  // cerr << endl;
  if (neg[n - 1] == 1)
    cout << -1 << endl;
  else
    cout << max(d[n - 1], 0LL) << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://abc137.contest.atcoder.jp/tasks/abc137_b


Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 998244353;

struct edge {
  int from;
  int to;
  int cost;
};
int n, m;
int d[2510];

signed main() {
  vector<edge> es;

  int n, m, p;
  cin >> n >> m >> p;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e1 = {a - 1, b - 1, c - p};
    es.push_back(e1);
  }

  fill(d, d + n, -INF);
  d[0] = 0;
  rep(i, n) {
    rep(j, m) {
      struct edge e = es[j];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
      }
    }
  }
  int neg[2510] = {0};
  rep(i, n) {
    rep(j, m) {
      struct edge e = es[j];
      if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        neg[e.to] = 1;
      }
    }
  }
  // rep(i,n) cerr << d[i] << " ";
  // cerr << endl;
  if (neg[n - 1] == 1)
    cout << -1 << endl;
  else
    cout << max(d[n - 1], 0LL) << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://abc137.contest.atcoder.jp/tasks/abc137_b