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

ll ME = 11;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;

void bf() {
  score[1] = 0;
  repr(i, 2, n + 1) { score[i] = INF; }
  for (ll i = 0;; i++) {
    bool finf = 0;
    bool finfv = 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 (v == n) {
          finfv = 1;
        }
      }
    }
    if (!finf) {
      ans = score[n];
      return;
    }
    if (finfv && i >= n) {
      ans = INF;
      return;
    }
    if (i >= 3 * n) {
      ans = 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 (ans < INF) {
    cout << max(-score[n], 0LL) << endl;
  } else {
    cout << -1 << 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

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

void bf() {
  score[1] = 0;
  repr(i, 2, n + 1) { score[i] = INF; }
  for (ll i = 0;; i++) {
    bool finf = 0;
    bool finfv = 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 (v == n) {
          finfv = 1;
        }
      }
    }
    if (!finf) {
      ans = score[n];
      return;
    }
    if (finfv && i >= n) {
      ans = INF;
      return;
    }
    if (i >= 3 * n) {
      ans = 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 (ans < INF) {
    cout << max(-score[n], 0LL) << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}