Language: C++

#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

struct Edge {
    int dst;
    int weight;
    bool operator<(const Edge& a) const {
        return weight < a.weight;
    }
};

int start[100010];
int dist[100010];
vector<Edge> edges[100010];
int used[100010];

int main() {
    int N, M;
    cin >> N >> M;

    if (M == 0) {
        cout << "Yes" << endl;
        return 0;
    }

    for (int i=0; i<M; i++) {
        int l, r, d;
        cin >> l >> r >> d;
        edges[l].push_back( Edge{r,d} );
        edges[r].push_back( Edge{l,-d} );
        // 始点を探し出す
        start[r] = 2; // 始点の可能性なし
        start[l] = start[l] == 2 ? 2 : 1; // 始点の可能性あり
    }

    // 始点を探す
    vector<int> sv;
    for (int i=1; i<=N; i++) {
        if (start[i] == 1) {
            sv.push_back(i);
        }
    }
    if (sv.size() == 0) {
        cout << "No" << endl;
        return 0;
    }

    for (int i=0; i<=N; i++) {
        dist[i] = INT_MAX;
        used[i] = -1;
    }
    // 適当に始点からはじめる
    for (int k=0; k<sv.size(); k++) {
        if (used[sv[k]] != -1) continue;
        queue<int> q;
        q.push( sv[k] );
        dist[sv[k]] = 0;

       while (!q.empty()) {
            int src = q.front();
            q.pop();

            if (used[src] != -1) continue;
            used[src] = k; // stage番号にする
            int curr_cost = dist[src];
            for (int i=0; i<edges[src].size(); i++) {
                Edge e = edges[src][i];
                if (dist[e.dst] == INT_MAX || dist[e.dst] == curr_cost + e.weight) {
                    dist[e.dst] = curr_cost + e.weight;
                    if (used[e.dst] == -1) {
                        q.push(e.dst);
                    }
                } else if (dist[e.dst] != INT_MAX && dist[e.dst] != curr_cost + e.weight) {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
 
        // マイナスの距離になってないか確認する
        int max_minus = 0;
        for (int i=1; i<=N; i++) {
            if (used[i] == k) {
                max_minus = min(max_minus, dist[i]);
            }
        }
        // 相対位置をずらす, このときに10^9を超えてないか確認
        for (int i=1; i<=N; i++) {
            if (dist[i] != INT_MAX && used[i] == k) {
                dist[i] += -1 * max_minus;
                if (dist[i] > 1000000000) {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
    }

    cout << "Yes" << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:19: error: 'INT_MAX' was not declared in this scope
   52 |         dist[i] = INT_MAX;
      |                   ^~~~~~~
a.cc:5:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    4 | #include <queue>
  +++ |+#include <climits>
    5 | using namespace std;
a.cc:71:36: error: 'INT_MAX' was not declared in this scope
   71 |                 if (dist[e.dst] == INT_MAX || dist[e.dst] == curr_cost + e.weight) {
      |                                    ^~~~~~~
a.cc:71:36: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:92:28: error: 'INT_MAX' was not declared in this scope
   92 |             if (dist[i] != INT_MAX && used[i] == k) {
      |                            ^~~~~~~
a.cc:92:28: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'