prithivMLmods/Coder-Stat huggingface dataset:
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, M, a, b, c;
  cin >> N >> M;
  vector<ll> A(N + 1), B(N + 1), C(N + 1), D(N + 1, -1e15);
  D[1] = 0;
  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c;
    A[i] = a;
    B[i] = b;
    C[i] = c;
  }
  for (int i = 1; i <= N - 1; i++) {
    for (int j = 1; j <= M; j++) {
      if (D[A[j]] == -1e15)
        continue;
      else if (D[B[j]] < D[A[j]] + C[j])
        D[B[j]] = D[A[j]] + C[j];
    }
  }
  vector<bool> E(N + 1, false);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (D[B[j]] < D[A[j]] + C[j]) {
        D[B[j]] = D[A[j]] + C[j];
        E[A[j]] = true, E[B[j]] = true;
      }
    }
  }
  if (E[N])
    cout << "inf" << endl;
  else
    cout << D[N] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, M, a, b, c;
  cin >> N >> M;
  vector<ll> A(M + 1), B(M + 1), C(M + 1), D(N + 1, -1e15);
  D[1] = 0;
  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c;
    A[i] = a;
    B[i] = b;
    C[i] = c;
  }
  for (int i = 1; i <= N - 1; i++) {
    for (int j = 1; j <= M; j++) {
      if (D[A[j]] == -1e15)
        continue;
      else if (D[B[j]] < D[A[j]] + C[j])
        D[B[j]] = D[A[j]] + C[j];
    }
  }
  vector<bool> E(N + 1, false);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (D[B[j]] < D[A[j]] + C[j]) {
        D[B[j]] = D[A[j]] + C[j];
        E[A[j]] = true, E[B[j]] = true;
      }
    }
  }
  if (E[N])
    cout << "inf" << endl;
  else
    cout << D[N] << endl;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll inf = (1LL << 60);

int N, M;
vector<ll> a(1005), b(1005), c(1005);
vector<ll> dist(1005, inf);

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] = -c[i];
  }

  dist[0] = 0;

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i]] == inf)
        continue;

      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
      }
    }
  }

  ll ans = -dist[N - 1];
  vector<bool> negative(N + 1, false);

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i]] == inf)
        continue;
      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
        negative[b[i]] = true;
      }

      if (negative[a[i]])
        negative[b[i]] = true;
    }
  }

  if (negative[N - 1])
    cout << "inf" << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll inf = (1LL << 60);

int N, M;
vector<ll> a(5005), b(5005), c(5005);
vector<ll> dist(1005, inf);

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] = -c[i];
  }

  dist[0] = 0;

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i]] == inf)
        continue;

      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
      }
    }
  }

  ll ans = -dist[N - 1];
  vector<bool> negative(N + 1, false);

  for (int loop = 0; loop < N; loop++) {
    for (int i = 0; i < M; i++) {
      if (dist[a[i]] == inf)
        continue;
      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
        negative[b[i]] = true;
      }

      if (negative[a[i]])
        negative[b[i]] = true;
    }
  }

  if (negative[N - 1])
    cout << "inf" << endl;
  else
    cout << ans << endl;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<pii, ll> pi3;
ll mod = 1000000007;
ll d[1010];
int main() {
  int num, way;
  scanf("%d%d", &num, &way);
  vector<pi3> v;
  for (int i = 0; i < num; i++) {
    int za, zb, zc;
    scanf("%d%d%d", &za, &zb, &zc);
    za--, zb--;
    v.push_back(make_pair(make_pair(za, zb), zc));
  }
  fill(d, d + num, -1000000000000000000LL);
  d[0] = 0;
  for (int p = 0; p < num * 4; p++) {
    ll t = d[num - 1];
    for (int i = 0; i < v.size(); i++)
      d[v[i].first.second] =
          max(d[v[i].first.second], d[v[i].first.first] + v[i].second);
    if (t != d[num - 1] && p > num * 2) {
      printf("inf\n");
      return 0;
    }
  }
  printf("%lld\n", d[num - 1]);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<pii, ll> pi3;
ll mod = 1000000007;
ll d[1010];
int main() {
  int num, way;
  scanf("%d%d", &num, &way);
  vector<pi3> v;
  for (int i = 0; i < way; i++) {
    int za, zb, zc;
    scanf("%d%d%d", &za, &zb, &zc);
    za--, zb--;
    v.push_back(make_pair(make_pair(za, zb), zc));
  }
  fill(d, d + num, -1000000000000000000LL);
  d[0] = 0;
  for (int p = 0; p < num * 4; p++) {
    ll t = d[num - 1];
    for (int i = 0; i < v.size(); i++)
      d[v[i].first.second] =
          max(d[v[i].first.second], d[v[i].first.first] + v[i].second);
    if (t != d[num - 1] && p > num * 2) {
      printf("inf\n");
      return 0;
    }
  }
  printf("%lld\n", d[num - 1]);
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
lint dp[1010];
int a[1010], b[1010], c[1010];
int main() {
  int n, m;
  lint out = 0, inf = 1E18 + 10;
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
  }
  rep(i, 1010) dp[i] = -inf;
  dp[0] = 0;
  rep(i, 5010) {
    int t = 0;
    rep(j, m) {
      // cout<<a[j]<<' '<<b[i]<<endl;
      if (dp[a[j]] + c[j] > dp[b[j]]) {
        dp[b[j]] = dp[a[j]] + c[j];
        if (b[j] == n - 1)
          t = 1;
      }
    }
    if (i > 4000 && t > 0) {
      cout << "inf" << endl;
      return 0;
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
lint dp[1010];
int a[2010], b[2010], c[2010];
int main() {
  int n, m;
  lint out = 0, inf = 1E18 + 10;
  cin >> n >> m;
  rep(i, m) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
  }
  rep(i, 1010) dp[i] = -inf;
  dp[0] = 0;
  rep(i, 5010) {
    int t = 0;
    rep(j, m) {
      // cout<<a[j]<<' '<<b[i]<<endl;
      if (dp[a[j]] + c[j] > dp[b[j]]) {
        dp[b[j]] = dp[a[j]] + c[j];
        if (b[j] == n - 1)
          t = 1;
      }
    }
    if (i > 4000 && t > 0) {
      cout << "inf" << endl;
      return 0;
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 12345678901234

struct edge {
  ll from, to, cost;
};
edge es[1010];
ll d[2020];
ll V, E;

ll bellmanford(ll s) {
  for (ll i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  ll cnt = 0;
  while (true) {
    bool update = false;
    cnt++;
    for (ll i = 0; i < E; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
        if (cnt > V) {
          d[e.to] = -INF;
          if (e.to == V - 1)
            return 1;
        }
      }
    }
    // if(!update) break;
    if (cnt > 2 * V)
      break;
  }
  return 0;
}

int main() {
  ll i, j, k;
  ll n, m;
  cin >> n >> m;
  V = n;
  E = m;
  for (i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    es[i] = edge{a, b, -c};
  }
  if (bellmanford(0) == 1)
    cout << "inf" << endl;
  else
    cout << -d[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
#define Mod 1000000007
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
#define INF 12345678901234

struct edge {
  ll from, to, cost;
};
edge es[2020];
ll d[1010];
ll V, E;

ll bellmanford(ll s) {
  for (ll i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  ll cnt = 0;
  while (true) {
    bool update = false;
    cnt++;
    for (ll i = 0; i < E; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
        if (cnt > V) {
          d[e.to] = -INF;
          if (e.to == V - 1)
            return 1;
        }
      }
    }
    // if(!update) break;
    if (cnt > 2 * V)
      break;
  }
  return 0;
}

int main() {
  ll i, j, k;
  ll n, m;
  cin >> n >> m;
  V = n;
  E = m;
  for (i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    es[i] = edge{a, b, -c};
  }
  if (bellmanford(0) == 1)
    cout << "inf" << endl;
  else
    cout << -d[n - 1] << endl;

  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using i64 = std::int_fast64_t;

struct edge {
  int from;
  int to;
  std::int_fast64_t cost;
};

const i64 INF = 1LL << 58;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<edge> graph(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    i64 c;
    cin >> a >> b >> c;
    a--, b--;
    edge e = {a, b, c};
    graph[i] = e;
  }

  vector<i64> d(N, -INF);
  d[0] = 0;

  for (int i = 0; i < N; i++) {
    for (auto e : graph) {
      if (d[e.from] != -INF)
        chmax(d[e.to], d[e.from] + e.cost);
    }
  }
  i64 res = d[N - 1];
  for (int i = 0; i < N; i++) {
    for (auto e : graph) {
      if (d[e.from] != -INF)
        chmax(d[e.to], d[e.from] + e.cost);
    }
  }

  if (res < d[N - 1]) {
    cout << "inf" << '\n';
  } else {
    cout << res << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using i64 = std::int_fast64_t;

struct edge {
  int from;
  int to;
  std::int_fast64_t cost;
};

const i64 INF = 1LL << 58;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<edge> graph(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    i64 c;
    cin >> a >> b >> c;
    a--, b--;
    edge e = {a, b, c};
    graph[i] = e;
  }

  vector<i64> d(N, -INF);
  d[0] = 0;

  for (int i = 0; i < N; i++) {
    for (auto e : graph) {
      if (d[e.from] != -INF)
        chmax(d[e.to], d[e.from] + e.cost);
    }
  }
  i64 res = d[N - 1];
  for (int i = 0; i < N; i++) {
    for (auto e : graph) {
      if (d[e.from] != -INF)
        chmax(d[e.to], d[e.from] + e.cost);
    }
  }

  if (res < d[N - 1]) {
    cout << "inf" << '\n';
  } else {
    cout << res << '\n';
  }
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

template <typename T> struct WG {
  using P = tuple<int, int, T>;
  int n;
  T inf = 2000000000000000000;
  vector<bool> negative_loop;
  vector<P> edges;
  vector<T> ans;
  WG(int n_) {
    n = n_;
    ans.resize(n, inf);
    negative_loop.resize(n, false);
  }
  void add_edge(int from, int to, T cost) {
    P edge = make_tuple(from, to, cost);
    edges.push_back(edge);
  }
  void bellmanford(int start) {
    ans[start] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < int(edges.size()) * 2; ++j) {
        int from = get<0>(edges[j]);
        int to = get<1>(edges[j]);
        T cost = get<2>(edges[j]);
        if (ans[from] < inf && ans[to] > ans[from] + cost) {
          ans[to] = ans[from] + cost;
          if (i > n - 2)
            negative_loop[to] = true;
        }
        if (negative_loop[from])
          negative_loop[to] = true;
      }
    }
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  WG<long> g(n);
  rep(i, m) {
    int a, b;
    long c;
    cin >> a >> b >> c;
    --a;
    --b;
    g.add_edge(a, b, -1 * c);
  }
  g.bellmanford(0);
  if (g.negative_loop[n - 1])
    cout << "inf\n";
  else
    cout << g.ans[n - 1] * -1 << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

template <typename T> struct WG {
  using P = tuple<int, int, T>;
  int n;
  T inf = 2000000000000000000;
  vector<bool> negative_loop;
  vector<P> edges;
  vector<T> ans;
  WG(int n_) {
    n = n_;
    ans.resize(n, inf);
    negative_loop.resize(n, false);
  }
  void add_edge(int from, int to, T cost) {
    P edge = make_tuple(from, to, cost);
    edges.push_back(edge);
  }
  void bellmanford(int start) {
    ans[start] = 0;
    for (int i = 0; i < n * 2; ++i) {
      for (int j = 0; j < edges.size(); ++j) {
        int from = get<0>(edges[j]);
        int to = get<1>(edges[j]);
        T cost = get<2>(edges[j]);
        if (ans[from] < inf && ans[to] > ans[from] + cost) {
          ans[to] = ans[from] + cost;
          if (i > n - 2)
            negative_loop[to] = true;
        }
        if (negative_loop[from])
          negative_loop[to] = true;
      }
    }
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  WG<long> g(n);
  rep(i, m) {
    int a, b;
    long c;
    cin >> a >> b >> c;
    --a;
    --b;
    g.add_edge(a, b, -1 * c);
  }
  g.bellmanford(0);
  if (g.negative_loop[n - 1])
    cout << "inf\n";
  else
    cout << g.ans[n - 1] * -1 << "\n";
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
// const ll INF = 1000000000000000000ll;
const ll INF = 9223372036854775807ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;
#define NOEDGE 1000000009

vector<pair<ll, ll>> e[1005];
ll score[1005];
ll n, m;
bool f = true;

void bellman_ford(int s) {
  for (int i = 0; i < n; i++)
    score[i] = -INF;
  score[s] = 0;
  ll cnt = 0;
  while (true) {
    bool update = false;
    ll bef = score[n - 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < e[i].size(); j++) {
        auto to = e[i][j].first;
        auto cost = e[i][j].second;
        if (score[i] != -INF && score[to] < score[i] + cost) {
          score[to] = score[i] + cost;
          update = true;
        }
      }
    }
    if (!update)
      break;
    cnt++;
    if (bef != score[n - 1] && cnt > n * 5) {
      f = false;
      break;
    }
  }
}

int main(void) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    e[a].pb(make_pair(b, c));
  }

  bellman_ford(0);

  /*
  for(int i=0; i<n; i++){
          cout << score[i] << endl;
  }
  */
  if (f) {
    cout << score[n - 1] << endl;
  } else {
    cout << "inf" << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
// const ll INF = 1000000000000000000ll;
const ll INF = 9223372036854775807ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;
#define NOEDGE 1000000009

vector<pair<ll, ll>> e[1005];
ll score[1005];
ll n, m;
bool f = true;

void bellman_ford(int s) {
  for (int i = 0; i < n; i++)
    score[i] = -INF;
  score[s] = 0;
  ll cnt = 0;
  while (true) {
    bool update = false;
    ll bef = score[n - 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < e[i].size(); j++) {
        auto to = e[i][j].first;
        auto cost = e[i][j].second;
        if (score[i] != -INF && score[to] < score[i] + cost) {
          score[to] = score[i] + cost;
          update = true;
        }
      }
    }
    if (!update)
      break;
    cnt++;
    if (cnt > n * 5) {
      if (bef != score[n - 1]) {
        f = false;
      }
      break;
    }
  }
}

int main(void) {
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    e[a].pb(make_pair(b, c));
  }

  bellman_ford(0);

  /*
  for(int i=0; i<n; i++){
          cout << score[i] << endl;
  }
  */
  if (f) {
    cout << score[n - 1] << endl;
  } else {
    cout << "inf" << endl;
  }

  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 10000000000000000ll;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> a(M), b(M), c(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] *= -1;
  }
  vector<ll> cost(N, INF);
  cost[0] = 0;
  bool flag = true;
  for (int i = 0; i < N; i++) {
    bool tmp = true;
    for (int j = 0; j < M; j++) {
      if (cost[a[j]] != INF && cost[b[j]] > cost[a[j]] + c[j]) {
        tmp = false;
        cost[b[j]] = cost[a[j]] + c[j];
      }
    }
    if (tmp) {
      flag = false;
      break;
    }
  }
  assert(cost[N - 1] == INF);
  if (flag) {
    ll res = cost[N - 1];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (cost[a[j]] != INF && cost[b[j]] > cost[a[j]] + c[j]) {
          cost[b[j]] = cost[a[j]] + c[j];
        }
      }
    }
    if (cost[N - 1] == res) {
      cout << -res << endl;
    } else {
      cout << "inf" << endl;
    }
  } else {
    cout << -cost[N - 1] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 10000000000000000ll;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> a(M), b(M), c(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] *= -1;
  }
  vector<ll> cost(N, INF);
  cost[0] = 0;
  bool flag = true;
  for (int i = 0; i < N; i++) {
    bool tmp = true;
    for (int j = 0; j < M; j++) {
      if (cost[a[j]] != INF && cost[b[j]] > cost[a[j]] + c[j]) {
        tmp = false;
        cost[b[j]] = cost[a[j]] + c[j];
      }
    }
    if (tmp) {
      flag = false;
      break;
    }
  }
  assert(cost[N - 1] != INF);
  if (flag) {
    ll res = cost[N - 1];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (cost[a[j]] != INF && cost[b[j]] > cost[a[j]] + c[j]) {
          cost[b[j]] = cost[a[j]] + c[j];
        }
      }
    }
    if (cost[N - 1] == res) {
      cout << -res << endl;
    } else {
      cout << "inf" << endl;
    }
  } else {
    cout << -cost[N - 1] << endl;
  }
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;

const int L = 1001;

const LL inf = 1LL << 60;
int N, M;
LL dist[L];
LL a[L];
LL b[L];
LL c[L];
int main() {
  scanf("%d%d", &N, &M);

  for (int i = 0; i < M; i++) {
    scanf("%lld%lld%lld", a + i, b + i, c + i);
    a[i]--;
    b[i]--;
  }
  fill(dist, dist + N, -inf);
  dist[0] = 0;
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 0; j < M; j++) {
      if (dist[a[j]] != -inf && dist[b[j]] < dist[a[j]] + c[j]) {
        dist[b[j]] = dist[a[j]] + c[j];
        if (b[j] == N - 1 && i >= N) {
          puts("inf");
          return 0;
        }
      }
    }
  }
  printf("%lld\n", dist[N - 1]);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;

const int L = 2001;

const LL inf = 1LL << 60;
int N, M;
LL dist[L];
LL a[L];
LL b[L];
LL c[L];
int main() {
  scanf("%d%d", &N, &M);

  for (int i = 0; i < M; i++) {
    scanf("%lld%lld%lld", a + i, b + i, c + i);
    a[i]--;
    b[i]--;
  }
  fill(dist, dist + N, -inf);
  dist[0] = 0;
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 0; j < M; j++) {
      if (dist[a[j]] != -inf && dist[b[j]] < dist[a[j]] + c[j]) {
        dist[b[j]] = dist[a[j]] + c[j];
        if (b[j] == N - 1 && i >= N) {
          puts("inf");
          return 0;
        }
      }
    }
  }
  printf("%lld\n", dist[N - 1]);

  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, m, a, b, c, cnt[1000], memo[1000];
vector<pair<int, int>> v[1000];
priority_queue<pair<int, int>> pq;

signed main() {
  rep(i, 1000) memo[i] = -INF;
  cin >> n >> m;
  rep(i, m) cin >> a >> b >> c, a--, b--, v[a].pb(mp(c, b));
  rep(i, v[0].size()) pq.push(v[0][i]);
  while (!pq.empty()) {
    int cost = pq.top().fi, to = pq.top().se;
    pq.pop();
    if (memo[to] >= cost)
      continue;
    memo[to] = cost;
    cnt[to]++;
    if (cnt[n - 1] > n) {
      cout << "inf" << endl;
      return 0;
    }
    rep(i, v[to].size()) pq.push(mp(v[to][i].fi + cost, v[to][i].se));
  }
  cout << memo[n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, m, a, b, c, cnt[1000], memo[1000];
vector<pair<int, int>> v[1000];
priority_queue<pair<int, int>> pq;

signed main() {
  rep(i, 1000) memo[i] = -INF;
  cin >> n >> m;
  rep(i, m) cin >> a >> b >> c, a--, b--, v[a].pb(mp(c, b));
  rep(i, v[0].size()) pq.push(v[0][i]);
  while (!pq.empty()) {
    int cost = pq.top().fi, to = pq.top().se;
    pq.pop();
    if (memo[to] >= cost)
      continue;
    memo[to] = cost;
    cnt[to]++;
    if (cnt[n - 1] > n) {
      cout << "inf" << endl;
      return 0;
    }
    rep(i, v[to].size()) if (cnt[v[to][i].se] <= n * 5)
        pq.push(mp(v[to][i].fi + cost, v[to][i].se));
  }
  cout << memo[n - 1] << endl;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef pair<int, ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
const int MD = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

vector<vector<pii>> r;

vector<ll> bellman(int s) {
  int n = sz(r);
  const ll inf = 1LL << 60;
  vector<ll> ds(n, -inf);
  ds[s] = 0;
  for (int i = 0, nxt = 1; nxt > 0; i++) {
    nxt = 0;
    rep(j, n) for (auto z : r[j]) {
      if (ds[j] != -inf && ds[z.first] < ds[j] + z.second) {
        ds[z.first] = ds[j] + z.second;
        nxt = 1;
        // dbg(i,j,z.first);
        if (i > n && z.first == n - 1) {
          ds[0] = -1;
          return ds;
        }
      }
    }
  }
  return ds;
}

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  r.resize(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    r[a - 1].emplace_back(b - 1, c);
  }
  auto rv = bellman(0);
  if (rv[0] == -1)
    cout << "inf\n";
  else
    cout << rv[N - 1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef pair<int, ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
const int MD = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

vector<vector<pii>> r;

vector<ll> bellman(int s) {
  int n = sz(r);
  const ll inf = 1LL << 60;
  vector<ll> ds(n, -inf);
  ds[s] = 0;
  for (int i = 0, nxt = 1; i < n + 30 && nxt > 0; i++) {
    nxt = 0;
    rep(j, n) for (auto z : r[j]) {
      if (ds[j] != -inf && ds[z.first] < ds[j] + z.second) {
        ds[z.first] = ds[j] + z.second;
        nxt = 1;
        // dbg(i,j,z.first);
        if (i > n && z.first == n - 1) {
          ds[0] = -1;
          return ds;
        }
      }
    }
  }
  return ds;
}

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  r.resize(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    r[a - 1].emplace_back(b - 1, c);
  }
  auto rv = bellman(0);
  if (rv[0] == -1)
    cout << "inf\n";
  else
    cout << rv[N - 1] << "\n";
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  ll path[N][3];
  rep(i, N) {
    cin >> path[i][0] >> path[i][1] >> path[i][2];
    path[i][0]--;
    path[i][1]--;
  }

  ll s_max[N], s_prev[N];
  ll s_1, s_2;
  rep(i, N) s_max[i] = -1 * pow(2, 40);
  s_max[0] = 0;
  rep(i, M) {
    rep(i, N) s_prev[i] = s_max[i];
    for (auto x : path)
      s_max[x[1]] = max(s_max[x[1]], s_prev[x[0]] + x[2]);
  }
  s_1 = s_max[N - 1];
  rep(i, M) {
    rep(i, N) s_prev[i] = s_max[i];
    for (auto x : path)
      s_max[x[1]] = max(s_max[x[1]], s_prev[x[0]] + x[2]);
  }
  s_2 = s_max[N - 1];

  if (s_1 >= s_2)
    cout << s_1 << endl;
  else
    cout << "inf" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  ll path[M][3];
  rep(i, M) {
    cin >> path[i][0] >> path[i][1] >> path[i][2];
    path[i][0]--;
    path[i][1]--;
  }

  ll s_max[N], s_prev[N];
  ll s_1, s_2;
  rep(i, N) s_max[i] = -1 * pow(2, 40);
  s_max[0] = 0;
  rep(i, M) {
    rep(i, N) s_prev[i] = s_max[i];
    for (auto x : path)
      s_max[x[1]] = max(s_max[x[1]], s_prev[x[0]] + x[2]);
  }
  s_1 = s_max[N - 1];
  rep(i, M) {
    rep(i, N) s_prev[i] = s_max[i];
    for (auto x : path)
      s_max[x[1]] = max(s_max[x[1]], s_prev[x[0]] + x[2]);
  }
  s_2 = s_max[N - 1];

  if (s_1 >= s_2)
    cout << s_1 << endl;
  else
    cout << "inf" << endl;
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
const ll INF = LLONG_MAX;
#define MAX_V 1010

ll V, E;
struct edge {
  int from;
  int to;
  ll cost;
};
edge es[MAX_V * MAX_V];
ll d[MAX_V];

// sからの最短経路  O(NM)
void shortest_path(int s) {
  for (int i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  while (true) {
    bool update = false;
    for (int i = 0; i < E; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

bool find_negative_loop() {
  memset(d, 0LL, sizeof(d));
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < E; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == V - 1 && e.to == V - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    a--, b--;
    es[i] = (edge){a, b, -c};
  }

  if (find_negative_loop()) {
    cout << "inf" << endl;
    return 0;
  }

  shortest_path(0);
  cout << -d[V - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
const ll INF = LLONG_MAX;
#define MAX_V 1010

ll V, E;
struct edge {
  int from;
  int to;
  ll cost;
};
edge es[MAX_V * MAX_V];
ll d[MAX_V];

// sからの最短経路  O(NM)
void shortest_path(int s) {
  for (int i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  for (int j = 0; j < V - 1; j++) {
    bool update = false;
    for (int i = 0; i < E; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

bool find_negative_loop() {
  memset(d, 0LL, sizeof(d));
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < E; j++) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == V - 1 && e.to == V - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    a--, b--;
    es[i] = (edge){a, b, -c};
  }

  if (find_negative_loop()) {
    cout << "inf" << endl;
    return 0;
  }

  shortest_path(0);
  cout << -d[V - 1] << endl;

  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Edge {
  ll cost;
  int from, to;
  Edge() {}
  Edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {}
};

static const int MAX = 1000;
static const ll INF = 1000000000000000LL;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int N, M;
Edge e[MAX];
ll d[MAX];

bool bellman_ford() {
  for (int i = 0; i < MAX; i++) {
    d[i] = INF;
  }
  // 頂点 0 からスタート
  d[0] = 0;
  for (int i = 0; i < 2 * N; i++) {
    for (int j = 0; j < M; j++) {
      if (d[e[j].from] != INF && d[e[j].to] > d[e[j].from] + e[j].cost) {
        d[e[j].to] = d[e[j].from] + e[j].cost;
        if (i >= N - 1 && e[j].to == N - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    e[i] = Edge(a - 1, b - 1, -c);
  }
  if (bellman_ford()) {
    cout << "inf\n";
  } else {
    cout << -d[N - 1] << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Edge {
  ll cost;
  int from, to;
  Edge() {}
  Edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {}
};

static const int MAX = 1000;
static const ll INF = 1000000000000000LL;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int N, M;
Edge e[2 * MAX];
ll d[MAX];

bool bellman_ford() {
  for (int i = 0; i < MAX; i++) {
    d[i] = INF;
  }
  // 頂点 0 からスタート
  d[0] = 0;
  for (int i = 0; i < 2 * N; i++) {
    for (int j = 0; j < M; j++) {
      if (d[e[j].from] != INF && d[e[j].to] > d[e[j].from] + e[j].cost) {
        d[e[j].to] = d[e[j].from] + e[j].cost;
        if (i >= N - 1 && e[j].to == N - 1)
          return true;
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    e[i] = Edge(a - 1, b - 1, -c);
  }
  if (bellman_ford()) {
    cout << "inf\n";
  } else {
    cout << -d[N - 1] << "\n";
  }
  return 0;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;

struct edge {
  int from, to;
  int64_t cost;
  edge(int a = 0, int b = 0, int64_t c = 0LL) : from(a), to(b), cost(c) {}
};

// サイズ,辺の情報,初期化の必要あり
vector<edge> es;

const int64_t INF = INT64_MAX / 10LL;

// sは始点,vは頂点数
vector<int64_t> bellman(int s, int v) {
  vector<int64_t> d(v, INF);
  int E = es.size();
  d[s] = 0LL;
  int cou = 0;
  while (true) {
    bool update = false;
    bool ch = false;
    for (int i = 0; i < E; ++i) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
        if (e.to == v - 1)
          ch = true;
      }
    }
    if (!update)
      break;
    if (cou == 2 * v && ch) {
      d[s] = INF;
      break;
    }
    cou++;
  }
  return d;
}

int main() {
  int n, m;
  cin >> n >> m;
  es.resize(m);
  rep(i, m) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    --a;
    --b;
    es[i] = edge(a, b, -c);
  }

  vector<ll> d = bellman(0, n);
  if (d[0] == INF) {
    cout << "inf" << endl;
  } else {
    cout << -d[n - 1] << endl;
  }
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;

struct edge {
  int from, to;
  int64_t cost;
  edge(int a = 0, int b = 0, int64_t c = 0LL) : from(a), to(b), cost(c) {}
};

// サイズ,辺の情報,初期化の必要あり
vector<edge> es;

const int64_t INF = INT64_MAX / 10LL;

// sは始点,vは頂点数
vector<int64_t> bellman(int s, int v) {
  vector<int64_t> d(v, INF);
  int E = es.size();
  d[s] = 0LL;
  int cou = 0;
  while (true) {
    bool update = false;
    bool ch = false;
    for (int i = 0; i < E; ++i) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
        if (e.to == v - 1)
          ch = true;
      }
    }
    if (!update)
      break;
    if (cou == 2 * v) {
      if (ch)
        d[s] = INF;
      break;
    }
    cou++;
  }
  return d;
}

int main() {
  int n, m;
  cin >> n >> m;
  es.resize(m);
  rep(i, m) {
    int a, b;
    ll c;
    cin >> a >> b >> c;
    --a;
    --b;
    es[i] = edge(a, b, -c);
  }

  vector<ll> d = bellman(0, n);
  if (d[0] == INF) {
    cout << "inf" << endl;
  } else {
    cout << -d[n - 1] << endl;
  }
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
int n, m, p[1005];
long long dist[1005];
vector<ii> adj[1005];

bool hasNegativeCycle = false;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    adj[a].push_back(ii(b, -c));
  }
  for (int i = 1; i <= n; i++)
    dist[i] = 1000000000000000007;
  dist[1] = 0;
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 0; i < n - 1; i++) { // relax all edges N-1 times
    for (int u = 1; u <= n; u++) {
      for (auto v : adj[u]) {
        if (dist[u] + v.second < dist[v.first])
          p[v.first] = u;
        dist[v.first] =
            min(dist[v.first], dist[u] + v.second); // node, distance
      }
    }
  }
  set<int> si;
  int cur = n;
  while (cur != 1) {
    si.insert(cur);
    cur = p[cur];
  }
  for (int u = 1; u <= n; u++) {
    for (auto v : adj[u]) {
      if (dist[v.first] > dist[u] + v.second &&
          dist[u] != 1000000000000000007 && (si.find(v.first) != si.end())) {
        hasNegativeCycle = true;
      }
    }
  }
  if (hasNegativeCycle == true)
    printf("inf");
  else
    printf("%lld", -dist[n]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
int n, m, p[1005];
long long dist[1005];
vector<ii> adj[1005];

bool hasNegativeCycle = false;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    adj[a].push_back(ii(b, -c));
  }
  for (int i = 1; i <= n; i++)
    dist[i] = 1000000000000000007;
  dist[1] = 0;
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 0; i < n - 1; i++) { // relax all edges N-1 times
    for (int u = 1; u <= n; u++) {
      for (auto v : adj[u]) {
        if (dist[u] + v.second < dist[v.first])
          p[v.first] = u;
        dist[v.first] =
            min(dist[v.first], dist[u] + v.second); // node, distance
      }
    }
  }
  set<int> si;
  int cur = n;
  while (cur != 1) {
    if (si.find(cur) != si.end())
      break;
    si.insert(cur);
    cur = p[cur];
  }
  for (int u = 1; u <= n; u++) {
    for (auto v : adj[u]) {
      if (dist[v.first] > dist[u] + v.second &&
          dist[u] != 1000000000000000007 && (si.find(v.first) != si.end())) {
        hasNegativeCycle = true;
      }
    }
  }
  if (hasNegativeCycle == true)
    printf("inf");
  else
    printf("%lld", -dist[n]);
}
Problem p03722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll INF = 1LL << 50;
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n), b(n), c(n);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] = -c[i];
  }
  vector<ll> dist(n);

  for (int i = 0; i < n; i++) {
    dist[i] = INF;
  }
  dist[0] = 0;
  for (int loop = 0; loop < n - 1; loop++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i])
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
    }
  }

  ll ans = dist[n - 1];

  vector<bool> negative(n, false);
  for (int loop = 0; loop < n; loop++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i] - 1] == INF)
        continue;
      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative[b[i] - 1] = true;
      }

      if (negative[a[i] - 1])
        negative[b[i] - 1] = true;
    }
  }

  if (negative[n - 1])
    cout << "inf" << endl;
  else
    cout << -dist[n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll INF = 1LL << 50;
int main() {
  int n, m;
  cin >> n >> m;
  int nmax = 1000, mmax = 2000;
  vector<ll> a(m), b(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] = -c[i];
  }
  vector<ll> dist(n);

  for (int i = 0; i < n; i++) {
    dist[i] = INF;
  }
  dist[0] = 0;
  for (int loop = 0; loop < n - 1; loop++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i] - 1] == INF)
        continue;

      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i])
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
    }
  }

  ll ans = dist[n - 1];

  vector<bool> negative(n, false);
  for (int loop = 0; loop < n; loop++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i] - 1] == INF)
        continue;
      if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
        dist[b[i] - 1] = dist[a[i] - 1] + c[i];
        negative[b[i] - 1] = true;
      }

      if (negative[a[i] - 1])
        negative[b[i] - 1] = true;
    }
  }

  if (negative[n - 1])
    cout << "inf" << endl;
  else
    cout << -dist[n - 1] << endl;
}
Problem p03722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int ll;

typedef std::pair<ll, int> P;

using namespace std;

ll INF = 1e18;

bool bell(vector<ll> &cost, vector<vector<P>> &E) {
  bool update = true;

  int N = E.size();

  vector<int> visit(0);

  visit.push_back(0);

  cost[0] = 0;

  int count = 0;

  while (update) {
    update = false;

    for (int v : visit) {

      for (P e : E[v]) {

        if (cost[e.second] == -INF) {

          visit.push_back(e.second);
        }

        if (cost[e.second] < cost[v] + e.first) {
          update = true;

          cost[e.second] = cost[v] + e.first;

          if (e.second == N - 1 && count > N) {

            return 0;
          }
        }
      }
    }

    count++;
  }

  return 1;
}

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

  vector<vector<P>> E(N, vector<P>(0));

  vector<ll> cost(N, -INF);

  for (int i = 0; i < M; i++) {

    ll a, b, c;
    cin >> a >> b >> c;
    E[a - 1].push_back(make_pair(c, b - 1));
  }

  if (bell(cost, E) == 0) {

    cout << "inf" << endl;
  } else {
    cout << cost[N - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;

typedef std::pair<ll, int> P;

using namespace std;

ll INF = 1e18;

bool bell(vector<ll> &cost, vector<vector<P>> &E) {
  bool update = true;

  int N = E.size();

  vector<int> visit(0);

  visit.push_back(0);

  cost[0] = 0;

  int count = 0;

  while (update) {
    update = false;

    for (int v : visit) {

      for (P e : E[v]) {

        if (cost[e.second] == -INF) {

          visit.push_back(e.second);
        }

        if (cost[e.second] < cost[v] + e.first) {
          update = true;

          cost[e.second] = cost[v] + e.first;

          if (e.second == N - 1 && count > N) {

            return 0;
          }
        }
      }
    }

    count++;

    if (count > N + 2) {

      return 1;
    }
  }

  return 1;
}

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

  vector<vector<P>> E(N, vector<P>(0));

  vector<ll> cost(N, -INF);

  for (int i = 0; i < M; i++) {

    ll a, b, c;
    cin >> a >> b >> c;
    E[a - 1].push_back(make_pair(c, b - 1));
  }

  if (bell(cost, E) == 0) {

    cout << "inf" << endl;
  } else {
    cout << cost[N - 1] << endl;
  }

  return 0;
}