Problem p02361 (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>;
//??????????????????
void dijkstra(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev) {
  const int V = g.size();
  priority_queue<Edge> q;
  dist[s] = 0;
  for (q.emplace(-2, s, 0); !q.empty();) {
    Edge e = q.top();
    q.pop();
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;
    // if(prev[t]!=-1)return;
    for (auto ge : g[e.dst]) {
      if (dist[ge.dst] > e.weight + ge.weight) {
        dist[ge.dst] = e.weight + ge.weight;
        q.emplace(ge.src, ge.dst, e.weight + ge.weight);
      }
    }
  }
}

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); //???????????§????????¢?????\\???
  vector<int> prev(V, -1);       //????????????????????\\???
  dijkstra(g, r, dist, prev);
  for (int i = 0; i < V; ++i) {
    if (dist[i] == INF64)
      cout << "INF" << endl;
    else
      cout << dist[i] << endl;
  }
}
// be checked by AOJ(GRL-1-A).

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>;
//??????????????????
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;
    }
  }
}
// be checked by AOJ(GRL-1-B).