Problem p02949 (C++)

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

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, long long M, long long P, std::vector<long long> A,
           std::vector<long long> B, std::vector<long long> C) {
  long long dist[2550] = {};
  bool reachable1[2550] = {};
  bool reachable2[2550] = {};

  reachable1[0] = true;
  reachable2[N - 1] = true;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      reachable1[B[j] - 1] |= reachable1[A[j] - 1];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      reachable2[A[j] - 1] |= reachable2[B[j] - 1];
    }
  }

  for (int i = 0; i < N; i++)
    dist[i] = -1e9;

  dist[0] = 0;

  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    vector<int> visit(N);
    for (int j = 0; j < M; j++) {
      visit[B[j] - 1] |= chmax(dist[B[j] - 1], dist[A[j] - 1] + C[j] - P);
    }
    if (i == N) {
      for (int j = 0; j < M; j++)
        if (visit[j] && reachable1[j] && reachable2[j]) {
          cout << -1 << endl;
          return;
        }
    }
  }
  cout << max(0ll, dist[N - 1]) << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  long long P;
  scanf("%lld", &P);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  std::vector<long long> C(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, M, P, std::move(A), std::move(B), std::move(C));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, long long M, long long P, std::vector<long long> A,
           std::vector<long long> B, std::vector<long long> C) {
  long long dist[2550] = {};
  bool reachable1[2550] = {};
  bool reachable2[2550] = {};

  reachable1[0] = true;
  reachable2[N - 1] = true;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      reachable1[B[j] - 1] |= reachable1[A[j] - 1];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      reachable2[A[j] - 1] |= reachable2[B[j] - 1];
    }
  }

  for (int i = 0; i < N; i++)
    dist[i] = -1e9;

  dist[0] = 0;

  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    vector<int> visit(N);
    for (int j = 0; j < M; j++) {
      visit[B[j] - 1] |= chmax(dist[B[j] - 1], dist[A[j] - 1] + C[j] - P);
    }
    if (i == N) {
      for (int j = 0; j < N; j++)
        if (visit[j] && reachable1[j] && reachable2[j]) {
          cout << -1 << endl;
          return;
        }
    }
  }
  cout << max(0ll, dist[N - 1]) << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  long long P;
  scanf("%lld", &P);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  std::vector<long long> C(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
    scanf("%lld", &C[i]);
  }
  solve(N, M, P, std::move(A), std::move(B), std::move(C));
  return 0;
}