Problem p02949 (C++)

Buggy submission (status: Runtime Error):
// ██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
// ██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
// ██████╔╝██║   ██║██║█████╗██║  ██║█████╗
// ██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
// ██║  ██║╚██████╔╝██║      ██████╔╝███████╗
// ╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ADD_OPERATORS_IN(T, COMP)                                              \
  bool operator<(const T &ot) const { return COMP(ot) == -1; }                 \
  bool operator>(const T &ot) const { return COMP(ot) == 1; }                  \
  bool operator==(const T &ot) const { return COMP(ot) == 0; }                 \
  bool operator!=(const T &ot) const { return COMP(ot) != 0; }

#define ADD_OPERATORS_OUT(T, COMP)                                             \
  bool operator<(const T &a, const T &b) const { return COMP(a, b) == -1; }    \
  bool operator>(const T &a, const T &b) const { return COMP(a, b) == 1; }     \
  bool operator==(const T &a, const T &b) const { return COMP(a, b) == 0; }    \
  bool operator!=(const T &a, const T &b) const { return COMP(a, b) != 0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)1e6 + 10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int)2e9;
const ll llinf = (ll)1e18;

struct edge {
  int a, b, cost;
};

int n, m, p;
vector<edge> e, e1;
// const int INF = 1000000000;

vector<int> g[maxn];
bool used[maxn];

void dfs(int v) {
  used[v] = 1;
  for (int to : g[v]) {
    if (used[to])
      continue;
    dfs(to);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  //  freopen("input.txt", "r", stdin);
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    e1.pb({a, b, -c});
    g[b].pb(a);
  }

  dfs(n - 1);
  for (auto &t : e1) {
    if (used[t.a] && used[t.b]) {
      e.pb(t);
    }
  }

  vector<int> d(n, INF);
  d[0] = 0;
  vector<int> p(n, -1);
  int x;
  for (int i = 0; i < n; ++i) {
    x = -1;
    for (int j = 0; j < m; ++j)
      if (d[e[j].a] < INF)
        if (d[e[j].b] > d[e[j].a] + e[j].cost) {
          d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
          p[e[j].b] = e[j].a;
          x = e[j].b;
        }
  }

  if (x != -1) {
    cout << -1;
  } else {
    cout << max(0, -d[n - 1]);
  }

  return 0;
}

Fixed submission:
// ██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
// ██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
// ██████╔╝██║   ██║██║█████╗██║  ██║█████╗
// ██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
// ██║  ██║╚██████╔╝██║      ██████╔╝███████╗
// ╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ADD_OPERATORS_IN(T, COMP)                                              \
  bool operator<(const T &ot) const { return COMP(ot) == -1; }                 \
  bool operator>(const T &ot) const { return COMP(ot) == 1; }                  \
  bool operator==(const T &ot) const { return COMP(ot) == 0; }                 \
  bool operator!=(const T &ot) const { return COMP(ot) != 0; }

#define ADD_OPERATORS_OUT(T, COMP)                                             \
  bool operator<(const T &a, const T &b) const { return COMP(a, b) == -1; }    \
  bool operator>(const T &a, const T &b) const { return COMP(a, b) == 1; }     \
  bool operator==(const T &a, const T &b) const { return COMP(a, b) == 0; }    \
  bool operator!=(const T &a, const T &b) const { return COMP(a, b) != 0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)1e6 + 10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int)2e9;
const ll llinf = (ll)1e18;

struct edge {
  int a, b, cost;
};

int n, m, p;
vector<edge> e, e1;
// const int INF = 1000000000;

vector<int> g[maxn];
bool used[maxn];

void dfs(int v) {
  used[v] = 1;
  for (int to : g[v]) {
    if (used[to])
      continue;
    dfs(to);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  //  freopen("input.txt", "r", stdin);
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    e1.pb({a, b, -c});
    g[b].pb(a);
  }

  dfs(n - 1);
  for (auto &t : e1) {
    if (used[t.a] && used[t.b]) {
      e.pb(t);
    }
  }
  m = sz(e);

  vector<int> d(n, INF);
  d[0] = 0;
  vector<int> p(n, -1);
  int x;
  for (int i = 0; i < n; ++i) {
    x = -1;
    for (int j = 0; j < m; ++j)
      if (d[e[j].a] < INF)
        if (d[e[j].b] > d[e[j].a] + e[j].cost) {
          d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
          p[e[j].b] = e[j].a;
          x = e[j].b;
        }
  }

  if (x != -1) {
    cout << -1;
  } else {
    cout << max(0, -d[n - 1]);
  }

  return 0;
}