Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

const ll ME = 12;
const ll MV = 12;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
vi finfv(MV, 0);

void bf() {
  score[1] = 0;
  repr(i, 2, n + 1) { score[i] = INF; }
  rep(i, n - 1) {
    bool finf = 0;
    rep(j, m) {
      ll u = a[j];
      ll v = b[j];
      if (score[v] > score[u] + c[j]) {
        score[v] = score[u] + c[j];
        finf = 1;
      }
    }
    if (!finf) {
      ans = score[n];
      return;
    }
    dump(score);
  }
  rep(i, n) {
    rep(j, m) {
      ll u = a[j];
      ll v = b[j];
      if (score[u] >= INF / 2)
        continue;
      if (score[v] > score[u] + c[j]) {
        score[v] = score[u] + c[j];
        finfv[v] = 1;
      }
      if (finfv[u]) {
        finfv[v] = 1;
      }
    }
    dump(score);
    dump(finfv);
  }
  ans = (finfv[n] ? INF : score[n]);
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll p;
  cin >> n >> m >> p;
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] -= p;
    c[i] = -c[i];
    g[a[i]].emplace_back(b[i], c[i]);
  }
  bf();
  if (finfv[n]) {
    cout << -1 << endl;
  } else {
    cout << max(-score[n], 0LL) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

const ll ME = 5050;
const ll MV = 5050;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
vi finfv(MV, 0);

void bf() {
  score[1] = 0;
  repr(i, 2, n + 1) { score[i] = INF; }
  rep(i, n - 1) {
    bool finf = 0;
    rep(j, m) {
      ll u = a[j];
      ll v = b[j];
      if (score[v] > score[u] + c[j]) {
        score[v] = score[u] + c[j];
        finf = 1;
      }
    }
    if (!finf) {
      ans = score[n];
      return;
    }
    dump(score);
  }
  rep(i, n) {
    rep(j, m) {
      ll u = a[j];
      ll v = b[j];
      if (score[u] >= INF / 2)
        continue;
      if (score[v] > score[u] + c[j]) {
        score[v] = score[u] + c[j];
        finfv[v] = 1;
      }
      if (finfv[u]) {
        finfv[v] = 1;
      }
    }
    dump(score);
    dump(finfv);
  }
  ans = (finfv[n] ? INF : score[n]);
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll p;
  cin >> n >> m >> p;
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] -= p;
    c[i] = -c[i];
    g[a[i]].emplace_back(b[i], c[i]);
  }
  bf();
  if (finfv[n]) {
    cout << -1 << endl;
  } else {
    cout << max(-score[n], 0LL) << endl;
  }
  return 0;
}