Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

constexpr long long INF64 = 1e18;

using Weight = long long;

struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else if (e.src != f.src)
    return e.src < f.src;
  else
    return e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;
//??????????????????
bool BellmanFord(const Graph &g, int s, vector<Weight> &dist,
                 vector<int> &prev) {
  const int V = g.size();
  bool negative_cycle = false;
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      for (auto e : g[j]) {
        if (dist[e.src] != INF64 && dist[e.dst] > dist[e.src] + e.weight) {
          dist[e.dst] = dist[e.src] + e.weight;
          prev[e.dst] = e.src;
          if (i == V - 1) {
            dist[e.dst] = -INF64;
            negative_cycle = true;
          }
        }
      }
    }
  }
  return negative_cycle;
}

int main() {
  cin.sync_with_stdio(false);
  int V, E, r;
  cin >> V >> E >> r;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    g[s].emplace_back(s, t, d);
  }
  vector<Weight> dist(V, INF64); //???????????§????????¢?????\\???
  dist[r] = 0;
  vector<int> prev(V, -1); //????????????????????\\???
  if (BellmanFord(g, r, dist, prev)) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    for (int i = 0; i < V; ++i) {
      if (dist[i] == INF64)
        cout << "INF" << endl;
      else
        cout << dist[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

constexpr long long INF64 = 1e18;

using Weight = long long;

struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else if (e.src != f.src)
    return e.src < f.src;
  else
    return e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;
//???????????????????????????????????????
long long BellmanFord(const Graph &g, int s, int t) {
  const int V = g.size();
  vector<Weight> dist(V, INF64); //???????????§????????¢?????\\???
  dist[s] = 0;
  vector<int> prev(V, -1); //????????????????????\\???
  bool negative_cycle = false;
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      for (auto e : g[j]) {
        if (dist[e.src] != INF64 && dist[e.dst] > dist[e.src] + e.weight) {
          dist[e.dst] = dist[e.src] + e.weight;
          prev[e.dst] = e.src;
          if (i == V - 1) {
            dist[e.dst] = -INF64;
            negative_cycle = true;
          }
        }
      }
    }
  }
  if (negative_cycle)
    return -INF64;
  return dist[t];
}

int main() {
  cin.sync_with_stdio(false);
  int V, E, r;
  cin >> V >> E >> r;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    g[s].emplace_back(s, t, d);
  }
  for (int i = 0; i < V; ++i) {
    long long ans = BellmanFord(g, r, i);
    if (ans == -INF64) {
      cout << "NEGATIVE CYCLE" << endl;
      break;
    }
    if (ans == INF64)
      cout << "INF" << endl;
    else
      cout << ans << endl;
  }
}