Language: C++


// E - Coins Respawn

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 9223372036854775807;
const int MAX_V = 2500;
const int MAX_E = 5000;

struct Edge {
	int from;
	int to;
	ll cost;
};

Edge edges[MAX_E];
ll min_d[MAX_V]; // 最短距離
int V, E;

void bellman_ford(int s) {
  bool negative_cycle = false;

	for (int i=0; i<V; i++) {
		min_d[i] = INF;
	}

	min_d[s] = 0;

	int update_cnt = 0;
	while (true) {
		bool update = false;
		for (int i=0; i<E; i++) {
			Edge e = edges[i];
			if (min_d[e.from] == INF) continue;
			if (min_d[e.to] > min_d[e.from] + e.cost) {
				min_d[e.to] = min_d[e.from] + e.cost;
				update = true;
			}
		}

		if (!update) break;
		else update_cnt++;

		if (update_cnt >= V) {
			negative_cycle = true;
			break;
		}
	}

	if (!negative_cycle) return;

	// set negative cycle to -INF
	update_cnt = 0;
	while (true) {
		bool update = false;
		for (int i=0; i<E; i++) {
			Edge e = edges[i];
			if (min_d[e.from] == INF || min_d[e.to] == -INF) continue;
			if (min_d[e.from] == -INF || min_d[e.to] > min_d[e.from] + e.cost) {
				min_d[e.to] = -INF;
				update = true;
			}
		}

		if (!update) break;
		else update_cnt++;
	}
}

int main() {
	int N, M, P;
	cin >> N >> M >> P;
	V = N;
	E = M;

	int A, B, C;
	for (int i=0; i<M; i++) {
		cin >> A >> B >> C;
		A--;
		B--;

		edges[i] = {A, B, -C+P};
	}

	bellman_ford(0);

	int ans;
	if (min_d[N-1] == -INF) {
		ans = -1;
	} else {
		ans = max(0, -min_d[N-1]);
	}

	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:92:26: error: no matching function for call to 'max(int, ll)'
   92 |                 ans = max(0, -min_d[N-1]);
      |                       ~~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:92:26: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   92 |                 ans = max(0, -min_d[N-1]);
      |                       ~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:92:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   92 |                 ans = max(0, -min_d[N-1]);
      |                       ~~~^~~~~~~~~~~~~~~~