Language: C++

#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "{";
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it != v.begin())
            os << ",";
        os << *it;
    }
    return os << "}";
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& v)
{
    os << "{";
    os << v.first << ", " << v.second;
    return os << "}";
}

// ---------------------------------------------------------------------------

using point = complex<double>;
using ll = long longint64_t;

struct Edge {
    int to, cost;
};

int my_main(int argc, char** argv)
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m, p;
    cin >> n >> m >> p;

    const ll inf = 100000000000000LL;
    vector<vector<Edge>> edges(n);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;

        int cos = c - p;
        edges[a].push_back(Edge{ b, -cos });
    }

    vector<bool> connectedToGoal(n);
    vector<bool> connectedToStart(n);
    {
        vector<vector<Edge>> rev(n);
        for (int s = 0; s < n; s++) {
            for (Edge e : edges[s]) {
                rev[e.to].push_back({ s, e.cost });
            }
        }
        queue<int> que;
        que.push(n - 1);

        while (!que.empty()) {
            int t = que.front();
            que.pop();

            if (connectedToGoal[t])
                continue;

            connectedToGoal[t] = true;
            for (Edge e : rev[t]) {
                que.push(e.to);
            }
        }
    }
    {
        queue<int> que;
        que.push(0);

        while (!que.empty()) {
            int t = que.front();
            que.pop();

            if (connectedToStart[t])
                continue;

            connectedToStart[t] = true;
            for (Edge e : edges[t]) {
                que.push(e.to);
            }
        }
    }

    vector<ll> dist(n, inf);
    dist[0] = 0;

    vector<int> prev(n, -1);

    for (int i = 0; i < n; i++) {
        for (int s = 0; s < n; s++) {
            for (Edge e : edges[s]) {
                if (dist[s] + e.cost < dist[e.to]) {
                    prev[e.to] = s;
                    dist[e.to] = dist[s] + e.cost;
                }
            }
        }
    }

    for (int s = 0; s < n; s++) {
        for (Edge e : edges[s]) {
            if (dist[s] + e.cost < dist[e.to]) {
                if (connectedToGoal[e.to] && connectedToStart[s]){
                    cout << -1 << endl;
                    dump("Negative cycle detected");
                    dump(s);
                    dump(e.to);
                    return 0;
                }
            }
        }
    }

    cout << max(0LL, -dist[n - 1]) << endl;
    //vector<vector<Edge>> edges(n);
    //for (int i = 0; i < m; i++) {
    //    int a, b, c;
    //    cin >> a >> b >> c;
    //    a--;
    //    b--;

    //    edges[a].push_back({ b, c - p });
    //}

    //ll inf = 1000000000000000LL;
    //vector<ll> maxi(n, -inf);
    //vector<ll> bef(n, -2);

    //using t = tuple<ll, int, int>;
    //priority_queue<t> queue;

    //queue.push({ 0LL, -1, 0 });
    //while (!queue.empty()) {
    //    t nex = queue.top();
    //    queue.pop();
    //    ll coin = get<0>(nex);
    //    int from = get<1>(nex);
    //    int to = get<2>(nex);

    //    bool ok = false;
    //    auto f = [&](ll c) {
    //        maxi[to] = c;
    //        bef[to] = from;
    //        ok = true;
    //    };
    //    if (maxi[to] == -inf){
    //        f(coin);
    //    } else if (maxi[to] == inf) {
    //        continue;
    //    } else {
    //        if (bef[to] == from) {
    //            if (maxi[to] < coin)
    //                f(inf);
    //        } else {
    //            if (maxi[to] < coin) {
    //                f(coin);
    //            }
    //        }
    //    }

    //    if (!ok)
    //        continue;

    //    for (Edge e : edges[to]) {
    //        if (maxi[to] == inf) {
    //            queue.push({ inf, to, e.to });
    //        } else {
    //            queue.push({ maxi[to]+e.coin, to, e.to });
    //        }
    //    }
    //}

    //dump(maxi);
    //if (maxi[n - 1] == inf) {
    //    cout << -1 << endl;
    //} else {
    //    ll p = maxi[n - 1];
    //    ll z = 0;
    //    cout << max(z, p) << endl;
    //}

    return 0;
}

// ----------------------------------------------------------------------------
// Test driver

#ifdef DEBUG
#define MAY_RUN_TESTCASE
#endif

#ifdef MAY_RUN_TESTCASE
#include <fstream>

#if __has_include(<unistd.h>)
// Mac, Linux
#include <unistd.h>
#elif __has_include(<io.h>)
// Windows
#include <io.h>
#else
// Other?
#error "unistd.h or io.h not found"
#endif

bool test_file_exists(const std::string& str)
{
    std::ifstream fs(str);
    return fs.is_open();
}

#endif

int main(int argc, char** argv)
{
#ifndef MAY_RUN_TESTCASE
    return my_main(argc, argv);
#else
    if (argc == 1) {
        return my_main(argc, argv);
    } else if (argc == 2) {

        char* stdin_file = argv[1];
        freopen(stdin_file, "r", stdin);
        return my_main(argc, argv);

    } else if (argc == 5) {

        std::string stdin_file = argv[1];
        std::string expected_file = argv[2];
        std::string stdout_file = argv[3];
        std::string stderr_file = argv[4];

        if (!test_file_exists(stdin_file)) {
            std::cerr << stdin_file << " not found" << std::endl;
            return 3;
        }

        if (!test_file_exists(expected_file)) {
            std::cerr << expected_file << " not found" << std::endl;
            return 3;
        }

        int original_stdin = dup(fileno(stdin));
        int original_stdout = dup(fileno(stdout));
        int original_stderr = dup(fileno(stderr));

        freopen(stdin_file.c_str(), "r", stdin);
        freopen(stdout_file.c_str(), "w", stdout);
        freopen(stderr_file.c_str(), "w", stderr);

        int ret = my_main(argc, argv);

        fflush(stdout);
        fflush(stderr);

        dup2(original_stderr, fileno(stderr));
        dup2(original_stdout, fileno(stdout));
        dup2(original_stdin, fileno(stdin));

        if (ret != 0) {
            std::cerr << "main returns " << ret << std::endl;
            return ret;
        }

        std::ifstream inp(stdin_file);
        std::ifstream out(stdout_file);
        std::ifstream err(stderr_file);
        std::ifstream exp(expected_file);

        // Clear is needed if the file is empty.
        std::cout << "----- input -----" << std::endl;
        std::cout << inp.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "----- output ----" << std::endl;
        std::cout << out.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "---- expected ---" << std::endl;
        std::cout << exp.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "----- stderr ----" << std::endl;
        std::cout << err.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl;

        inp.seekg(0);
        out.seekg(0);
        exp.seekg(0);
        err.seekg(0);

        std::string output_str, expected_str;
        {
            std::stringstream output_ss;
            output_ss << out.rdbuf();
            output_str = output_ss.str();

            std::stringstream expected_ss;
            expected_ss << exp.rdbuf();
            expected_str = expected_ss.str();
        }

        // Remove trailing spaces
        output_str.erase(output_str.find_last_not_of(" \n\r\t") + 1);
        expected_str.erase(expected_str.find_last_not_of(" \n\r\t") + 1);

        if (output_str == expected_str)
            return 0;
        else
            return 1;
    }
    return 1;
#endif
}


Compiler output:
a.cc:67:16: error: expected ';' before 'longint64_t'
   67 | using ll = long longint64_t;
      |                ^~~~~~~~~~~~
      |                ;
a.cc: In function 'int my_main(int, char**)':
a.cc:81:11: error: 'll' does not name a type; did you mean 'all'?
   81 |     const ll inf = 100000000000000LL;
      |           ^~
      |           all
a.cc:136:12: error: 'll' was not declared in this scope; did you mean 'all'?
  136 |     vector<ll> dist(n, inf);
      |            ^~
      |            all
a.cc:136:14: error: template argument 1 is invalid
  136 |     vector<ll> dist(n, inf);
      |              ^
a.cc:136:14: error: template argument 2 is invalid
a.cc:136:24: error: 'inf' was not declared in this scope; did you mean 'ynf'?
  136 |     vector<ll> dist(n, inf);
      |                        ^~~
      |                        ynf
a.cc:136:27: error: expression list treated as compound expression in initializer [-fpermissive]
  136 |     vector<ll> dist(n, inf);
      |                           ^
a.cc:137:9: error: invalid types 'int[int]' for array subscript
  137 |     dist[0] = 0;
      |         ^
a.cc:144:25: error: invalid types 'int[int]' for array subscript
  144 |                 if (dist[s] + e.cost < dist[e.to]) {
      |                         ^
a.cc:144:44: error: invalid types 'int[int]' for array subscript
  144 |                 if (dist[s] + e.cost < dist[e.to]) {
      |                                            ^
a.cc:146:25: error: invalid types 'int[int]' for array subscript
  146 |                     dist[e.to] = dist[s] + e.cost;
      |                         ^
a.cc:146:38: error: invalid types 'int[int]' for array subscript
  146 |                     dist[e.to] = dist[s] + e.cost;
      |                                      ^
a.cc:154:21: error: invalid types 'int[int]' for array subscript
  154 |             if (dist[s] + e.cost < dist[e.to]) {
      |                     ^
a.cc:154:40: error: invalid types 'int[int]' for array subscript
  154 |             if (dist[s] + e.cost < dist[e.to]) {
      |                                        ^
a.cc:166:27: error: invalid types 'int[int]' for array subscript
  166 |     cout << max(0LL, -dist[n - 1]) << endl;
      |                           ^