Language: C++

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <bitset>

using std::string;
using std::cin;
using std::cout;
typedef long long ll;
using std::vector;
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) {
    vec.resize(len);
    for (auto &v : vec) ndarray(v, args...);
}

//////////////////////////////////////

enum Status {
    Not = 0,
    Now,
    End,
};

template<typename Edge, typename T>
struct Graph {
    vector<vector<Edge>> g;
    vector<T> data;
    vector<Status> s;
    vector<Status> s2;
    
    Graph(int N) : g(N+1), data(N+1), s(N+1), s2(N+1) {}
    
    void add(int from, Edge e) {
        g[from].push_back(e);
    }
    void set_data(int node, T data) {
        data[node] = data;
    }
    int dfs(int now, int par, int depth, int score) {
        //std::cout << "searching now: " << now << std::endl;
        if (g[now].empty()) {
            //終端ノード
            if (now == 1) {
                return score;
            } else {
                return 0;
            }
        }
        int res = 0;
        s[now] = Status::Now;
        data[now] = score;
        for (auto e : g[now]) {
            int chi = e.to;
            if (s[chi] == Status::Now) {
                //閉路あり
                if (data[chi] < score+e.weight) {
                    //1まで繋がっていればINFそうで無ければ0
                    int tmp = dfs2(chi, -1, depth, 0);
                    if (tmp) {
                        return INT_MAX;
                    } else {
                        return 0;
                    }
                }
            } else {
                //未探索、親が複数
                int tmp = dfs(chi, now, depth+1, score+e.weight);
                if (tmp == INT_MAX) return INT_MAX;
                res = std::max(res, tmp);
            }
        }
        return res;
    }
    int dfs2(int now, int par, int depth, int score) {
        //std::cout << "searching now 2: " << now << std::endl;
        if (g[now].empty()) {
            //終端ノード
            if (now == 1) {
                return 1;
            } else {
                return 0;
            }
        }
        int res = 0;
        s2[now] = Status::Now;
        data[now] = score;
        for (auto e : g[now]) {
            int chi = e.to;
            if (s2[chi] == Status::Now) {
                //閉路は無視
            } else {
                //未探索、親が複数
                int tmp = dfs2(chi, now, depth+1, score);
                res = std::max(res, tmp);
            }
        }
        return res;
    }
};

struct Edge {
    int to;
    int weight;
    Edge(int to, int weight) : to(to), weight(weight) {}
};

//////////////////////////////////////

int main() {
    
    int N, M, P;
    cin >> N >> M >> P;
    
    //ゴールから逆算する
    //閉路を見つけた場合、閉路の長さ*Pが閉路での獲得コインを下回れば-1
    //そうでないなら1にたどり着いた時のスコアの最大値が答え
    
    Graph<Edge, int> g(N);
    
    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        g.add(b, Edge(a, c-P));
    }
    
    auto res = g.dfs(N, -1, 0, 0);
    if (res != INT_MAX) {
        cout << res << std::endl;
    } else {
        cout << -1 << std::endl;
    }
    
    return 0;
}



Compiler output:
a.cc: In member function 'int Graph<Edge, T>::dfs(int, int, int, int)':
a.cc:67:32: error: 'INT_MAX' was not declared in this scope
   67 |                         return INT_MAX;
      |                                ^~~~~~~
a.cc:11:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   10 | #include <bitset>
  +++ |+#include <climits>
   11 | 
a.cc:75:28: error: 'INT_MAX' was not declared in this scope
   75 |                 if (tmp == INT_MAX) return INT_MAX;
      |                            ^~~~~~~
a.cc:75:28: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int main()':
a.cc:134:16: error: 'INT_MAX' was not declared in this scope
  134 |     if (res != INT_MAX) {
      |                ^~~~~~~
a.cc:134:16: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'