Problem p02361 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#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 eb emplace_back
typedef long long ll;
#define int ll
auto const inf = numeric_limits<int>::max() / 4;

using Weight = int;
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>;

pair<vector<Weight>, bool> spfa(const Graph &g, int s) {
  const Weight inf = numeric_limits<Weight>::max() / 4;
  const int n = g.size();
  priority_queue<int> q;
  vector<Weight> dist(n, inf);
  vector<int> inQ(n);
  vector<int> count(n);
  dist[s] = 0;
  q.emplace(s);
  inQ[s] = true;
  ++count[s];
  bool negCycle = false;
  for (int i = 0; q.size(); i++) {
    int v = q.top();
    q.pop();
    inQ[v] = false;
    for (auto &e : g[v]) {
      if (dist[v] != inf && dist[e.dst] > dist[v] + e.weight) {
        dist[e.dst] = dist[v] + e.weight;
        if (!inQ[e.dst]) {
          q.emplace(e.dst);
          inQ[e.dst] = true;
          ++count[e.dst];
          if (count[e.dst] >= n) {
            negCycle = true;
            goto END;
          }
        }
      }
    }
  }
END:;
  return make_pair(dist, !negCycle);
}

signed main() {
  int V, E, r;
  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);
    }
    bool ok;
    vector<Weight> dist;
    tie(dist, ok) = spfa(g, r);
    if (ok) {
      for (int i = 0; i < (int)dist.size(); i++) {
        if (dist[i] == inf)
          cout << "INF";
        else
          cout << dist[i];
        cout << endl;
      }
    } else {
      cout << "NEGATIVE CYCLE" << endl;
    }
  }
}

Error: MLE

Fixed submission:

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

typedef ll Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src_, int dst_, Weight weight_)
      : src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

vector<Weight> dijkstra(const Graph &g, int s) {
  const size_t n = g.size();
  const Weight inf = numeric_limits<Weight>::max() / 4;
  Matrix m(n, Array(n, inf));
  for (auto &es : g)
    for (auto &e : es)
      m[e.src][e.dst] = min(m[e.src][e.dst], e.weight);
  vector<Weight> d(n, inf);
  vector<int> used(n);
  d[s] = 0;
  rep(i, n) {
    int v = -1;
    rep(j, n) if (!used[j] && (v == -1 || d[j] < d[v])) v = j;
    used[v] = true;
    /* if(min_v == t) return min_d; */
    for (auto &e : g[v]) {
      int u = e.dst;
      d[u] = min(d[u], d[v] + m[v][u]);
    }
  }
  return d;
}

int main() {
  int n, m, r;
  while (cin >> n >> m >> r) {
    Graph g(n);
    rep(i, m) {
      int s, t;
      Weight d;
      cin >> s >> t >> d;
      g[s].eb(s, t, d);
    }
    Array d = dijkstra(g, r);
    for (auto &x : d) {
      if (x >= 1e11)
        cout << "INF";
      else
        cout << x;
      cout << endl;
    }
  }
}