Language: C++

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X))
#define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X))
#define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++)
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define print(x) cout << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll INF = 9 * 1e15;
const ll MOD = 1e9 + 7;

class Edge {
public:
  ll from;
  ll to;
  ll cost;

  Edge() {}
  Edge(ll from, ll to, ll cost) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }

  bool operator<(const Edge &edge) const {
    return cost < edge.cost; //昇順
  }

  bool operator>(const Edge &edge) const {
    return cost > edge.cost; //降順(std::greater)
  }
};

class Graph {
public:
  ll nodes; // ノード数
  vector<Edge> edges;

  Graph() {}
  Graph(ll nodes) { this->nodes = nodes; }
  void addEdge(ll from, ll to, ll cost) {
    this->edges.push_back(Edge(from, to, cost));
  }
};

class Kruskal {
private:
  Graph graph;
  vector<Edge> MinimumSpanningTree;
  ll minimumCost;
  void Kruskal::searchMinimumSpanningTree() {
    UnionFindTree uf(graph.nodes);
    sort(all(graph.edges));
    itrep(edge, graph.edges) {
      if (!uf.same(edge->from, edge->to)) {
        uf.unite(edge->from, edge->to);
        MinimumSpanningTree.push_back(*edge);
      }
    }
  }

public:
  Kruskal::Kruskal(Graph graph) { this->graph = graph; }
  ll Kruskal::getMinimumSpanningTreeCost() {
    searchMinimumSpanningTree();
    ll cost = 0;
    itrep(it, MinimumSpanningTree) { cost += it->cost; }
    return cost;
  }
};

// ベルマンフォード O(|V||E|)
class BellmanFord {
private:
  Graph graph;
  // 閉路が含まれるかは個々のノードごとに管理する必要あり
  vector<bool> hasNegativeCycles;
  vector<ll> distances;

public:
  BellmanFord(Graph graph) {
    this->graph = graph;
    this->distances = vector<ll>(this->graph.nodes + 1, INF);
    this->hasNegativeCycles = vector<bool>(this->graph.nodes, false);
  }
  void searchMinimumPath(ll src) {
    this->distances[src] = 0;

    for (ll i = 0; i < graph.nodes - 1; i++) {
      itrep(edge, graph.edges) {
        ll u = edge->from;
        ll v = edge->to;
        ll w = edge->cost;
        if (this->distances[u] + w < this->distances[v]) {
          this->distances[v] = this->distances[u] + w;
        }
      }
    }

    itrep(edge, graph.edges) {
      ll u = edge->from;
      ll v = edge->to;
      ll w = edge->cost;
      if (this->distances[u] + w < this->distances[v]) {
        this->hasNegativeCycles[v] = true;
      }
      if (this->hasNegativeCycles[u] == true) {
        this->hasNegativeCycles[v] = true;
      }
    }
  }
  ll getDistance(ll n) { return this->distances[n]; }
  bool hasNegativeCycle(ll n) { return this->hasNegativeCycles[n]; }
};

void solve(long long N, long long M, std::vector<long long> a,
           std::vector<long long> b, std::vector<long long> c) {
  Graph g(N);
  rep(m, 0, M) { g.addEdge(a[m], b[m], -c[m]); }

  BellmanFord bellmanFord(g);
  bellmanFord.searchMinimumPath(1);

  if (!bellmanFord.hasNegativeCycle(N)) {
    print(-bellmanFord.getDistance(N));
  } else {
    print("inf");
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> a(M);
  std::vector<long long> b(M);
  std::vector<long long> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    scanf("%lld", &c[i]);
  }
  solve(N, M, std::move(a), std::move(b), std::move(c));
  return 0;
}


Compiler output:
a.cc:79:8: error: extra qualification 'Kruskal::' on member 'searchMinimumSpanningTree' [-fpermissive]
   79 |   void Kruskal::searchMinimumSpanningTree() {
      |        ^~~~~~~
a.cc:91:3: error: extra qualification 'Kruskal::' on member 'Kruskal' [-fpermissive]
   91 |   Kruskal::Kruskal(Graph graph) { this->graph = graph; }
      |   ^~~~~~~
a.cc:92:6: error: extra qualification 'Kruskal::' on member 'getMinimumSpanningTreeCost' [-fpermissive]
   92 |   ll Kruskal::getMinimumSpanningTreeCost() {
      |      ^~~~~~~
a.cc: In member function 'void Kruskal::searchMinimumSpanningTree()':
a.cc:80:5: error: 'UnionFindTree' was not declared in this scope
   80 |     UnionFindTree uf(graph.nodes);
      |     ^~~~~~~~~~~~~
a.cc:83:12: error: 'uf' was not declared in this scope
   83 |       if (!uf.same(edge->from, edge->to)) {
      |            ^~