Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  i64 p;
  scanf("%d%d%lld", &n, &m, &p);
  std::vector<int> loop, a(m), b(m);
  std::vector<i64> c(m);
  std::vector<std::vector<std::pair<int, i64>>> g(n), rg(n);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &a[i], &b[i], &c[i]);
    if (a[i] == b[i]) {
      a[i]--;
      b[i]--;
      loop.push_back(i);
      continue;
    }

    g[a[i] - 1].push_back({b[i] - 1, c[i] - p});
    rg[b[i] - 1].push_back({a[i] - 1, c[i] - p});
    a[i]--;
    b[i]--;
  }

  std::vector<bool> from0(n, false), from1(n, false);
  from0[0] = from1[n - 1] = true;
  std::queue<int> qu;
  qu.push(n - 1);
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();

    for (auto e : rg[p]) {
      if (from1[e.first])
        continue;
      from1[e.first] = true;
      qu.push(e.first);
    }
  }
  qu.push(0);
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();

    for (auto e : g[p]) {
      if (from0[e.first])
        continue;
      from0[e.first] = true;
      qu.push(e.first);
    }
  }

  for (auto i : loop) {
    if (c[i] - p <= 0)
      continue;
    if (c[i] - p > 0 and from0[a[i]] and from1[a[i]]) {
      printf("-1\n");
      return 0;
    }
  }

  std::vector<std::vector<std::pair<int, i64>>> G(n);
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i])
      continue;
    if (from0[a[i]] and from1[b[i]]) {
      G[a[i]].push_back({b[i], p - c[i]});
    }
  }

  bool isFinish = true;
  const i64 INF = 1LL << 60;
  std::vector<i64> dist(n, INF);
  dist[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int v = 0; v < n; v++) {
      for (auto e : G[v]) {
        if (dist[v] != INF and dist[e.first] > dist[v] + e.second) {
          dist[e.first] = dist[v] + e.second;
          if (i == n - 1)
            isFinish = false;
        }
      }
    }
  }
  if (!isFinish) {
    printf("-1\n");
    return 0;
  }

  printf("%lld\n", std::max(0LL, -dist[n - 1]));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  i64 p;
  scanf("%d%d%lld", &n, &m, &p);
  std::vector<int> loop, a(m), b(m);
  std::vector<i64> c(m);
  std::vector<std::vector<std::pair<int, i64>>> g(n), rg(n);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &a[i], &b[i], &c[i]);
    if (a[i] == b[i]) {
      a[i]--;
      b[i]--;
      loop.push_back(i);
      continue;
    }

    g[a[i] - 1].push_back({b[i] - 1, c[i] - p});
    rg[b[i] - 1].push_back({a[i] - 1, c[i] - p});
    a[i]--;
    b[i]--;
  }

  std::vector<bool> from0(n, false), from1(n, false);
  from0[0] = from1[n - 1] = true;
  std::queue<int> qu;
  qu.push(n - 1);
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();

    for (auto e : rg[p]) {
      if (from1[e.first])
        continue;
      from1[e.first] = true;
      qu.push(e.first);
    }
  }
  qu.push(0);
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();

    for (auto e : g[p]) {
      if (from0[e.first])
        continue;
      from0[e.first] = true;
      qu.push(e.first);
    }
  }

  for (auto i : loop) {
    if (c[i] - p <= 0)
      continue;
    if (c[i] - p > 0 and from0[a[i]] and from1[a[i]]) {
      printf("-1\n");
      return 0;
    }
  }

  std::vector<std::vector<std::pair<int, i64>>> G(n);
  for (int i = 0; i < m; i++) {
    if (a[i] == b[i])
      continue;
    if (from0[a[i]] and from1[b[i]]) {
      G[a[i]].push_back({b[i], p - c[i]});
    }
  }

  bool isFinish = true;
  const i64 INF = 1LL << 60;
  std::vector<i64> dist(n, INF);
  dist[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int v = 0; v < n; v++) {
      for (auto e : G[v]) {
        if (dist[v] != INF and dist[e.first] > dist[v] + e.second) {
          dist[e.first] = dist[v] + e.second;
          if (i == n - 1)
            isFinish = false;
        }
      }
    }
  }
  if (!isFinish) {
    printf("-1\n");
    return 0;
  }

  printf("%lld\n", std::max(0LL, -dist[n - 1]));
  return 0;
}