Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a - 1; i >= b; i--)

struct Edge {
  ll from;
  ll to;
  ll score;
};

int main() {
  ll N, M, P;
  cin >> N >> M >> P;
  vector<Edge> edges;
  rep(i, 0, M) {
    ll A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    edges.push_back({A, B, C - P});
  }
  vector<ll> score(N, numeric_limits<ll>::min());
  score[0] = 0;
  unordered_map<ll, bool> loop;
  unordered_map<ll, bool> inv_reachable;
  inv_reachable[N - 1] = true;
  rep(i, 0, 2 * M) {
    for (Edge e : edges) {
      if (score[e.from] != numeric_limits<ll>::min() &&
          score[e.from] + e.score > score[e.to]) {
        if (i >= M) {
          loop[e.to] = true;
        }
        score[e.to] = score[e.from] + e.score;
      }
      if (inv_reachable[e.to]) {
        inv_reachable[e.from] = true;
      }
    }
  }
  rep(i, 0, N) {
    if (loop[i] && inv_reachable[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << max((ll)0, score[N - 1]) << endl;
}


Fixed submission:
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a - 1; i >= b; i--)

struct Edge {
  ll from;
  ll to;
  ll score;
};

int main() {
  ll N, M, P;
  cin >> N >> M >> P;
  vector<Edge> edges;
  rep(i, 0, M) {
    ll A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    edges.push_back({A, B, C - P});
  }
  vector<ll> score(N, numeric_limits<ll>::min());
  score[0] = 0;
  vector<bool> loop(N);
  vector<bool> inv_reachable(N);
  inv_reachable[N - 1] = true;
  rep(i, 0, 2 * M) {
    for (Edge e : edges) {
      if (score[e.from] != numeric_limits<ll>::min() &&
          score[e.from] + e.score > score[e.to]) {
        if (i >= M) {
          loop[e.to] = true;
        }
        score[e.to] = score[e.from] + e.score;
      }
      if (inv_reachable[e.to]) {
        inv_reachable[e.from] = true;
      }
    }
  }
  rep(i, 0, N) {
    if (loop[i] && inv_reachable[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << max((ll)0, score[N - 1]) << endl;
}