Problem p02362 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, const vector<T> &);
template <size_t n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <size_t n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << make_tuple(__VA_ARGS__)                    \
        << " (L : " << __LINE__ << ")" << endl)
#else
#define dump(...)
#endif
#define all(c) begin(c), end(c)
#define range(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) range(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
int const mod = 1000000007;
auto const inf = numeric_limits<int>::max() / 8;

using Weight = long long;
using Capacity = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

int V, E, r;

pair<vector<Weight>, bool> bellmanFord(const Graph &g, int s) {
  int n = g.size();
  const Weight inf = numeric_limits<Weight>::max() / 8;
  Edges es;
  for (int i = 0; i < n; i++)
    for (auto &e : g[i])
      es.emplace_back(e);
  vector<Weight> dist(n, inf);
  dist[s] = 0;
  bool negCycle = false;
  for (int i = 0;; i++) {
    bool update = false;
    for (auto &e : es) {
      if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
        dist[e.dst] = dist[e.src] + e.weight;
        update = true;
      }
    }
    if (!update)
      break;
    if (i > V) {
      negCycle = true;
    }
  }
  return make_pair(dist, !negCycle);
}

int main() {
  while (cin >> V >> E >> r) {
    Graph g(V);
    rep(i, E) {
      int a, b, c;
      cin >> a >> b >> c;
      g[a].eb(a, b, c);
    }
    auto ans = bellmanFord(g, r);
    if (ans.second) {
      for (int i = 0; i < V; i++) {
        if (ans.first[i] > 1e10)
          cout << "INF";
        else
          cout << ans.first[i];
        cout << endl;
      }
    } else {
      cout << "NEGATIVE CYCLE" << endl;
    }
  }
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, const vector<T> &);
template <size_t n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <size_t n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << make_tuple(__VA_ARGS__)                    \
        << " (L : " << __LINE__ << ")" << endl)
#else
#define dump(...)
#endif
#define all(c) begin(c), end(c)
#define range(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) range(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
int const mod = 1000000007;
auto const inf = numeric_limits<int>::max() / 8;

using Weight = long long;
using Capacity = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

int V, E, r;

pair<vector<Weight>, bool> bellmanFord(const Graph &g, int s) {
  int n = g.size();
  const Weight inf = numeric_limits<Weight>::max() / 8;
  Edges es;
  for (int i = 0; i < n; i++)
    for (auto &e : g[i])
      es.emplace_back(e);
  vector<Weight> dist(n, inf);
  dist[s] = 0;
  bool negCycle = false;
  for (int i = 0;; i++) {
    bool update = false;
    for (auto &e : es) {
      if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
        dist[e.dst] = dist[e.src] + e.weight;
        update = true;
      }
    }
    if (!update)
      break;
    if (i > V) {
      negCycle = true;
      break;
    }
  }
  return make_pair(dist, !negCycle);
}

int main() {
  while (cin >> V >> E >> r) {
    Graph g(V);
    rep(i, E) {
      int a, b, c;
      cin >> a >> b >> c;
      g[a].eb(a, b, c);
    }
    auto ans = bellmanFord(g, r);
    if (ans.second) {
      for (int i = 0; i < V; i++) {
        if (ans.first[i] > 1e10)
          cout << "INF";
        else
          cout << ans.first[i];
        cout << endl;
      }
    } else {
      cout << "NEGATIVE CYCLE" << endl;
    }
  }
}