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

// #include {{{
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// }}}

using namespace std;

// macros {{{
template <typename T, size_t N> constexpr size_t NELEMS(T (&)[N]) { return N; }

template <typename InputIt> void PRINT_RANGE(InputIt first, InputIt last) {
  for (; first != last; ++first)
    cout << *first << (first == last ? "" : " ");
  cout << "\\n";
}

template <typename T> void PRINT_MATRIX(T mat, size_t nr, size_t nc) {
  for (size_t r = 0; r < nr; ++r) {
    for (size_t c = 0; c < nc; ++c) {
      cout << mat[r][c] << (c == nc - 1 ? "" : " ");
    }
    cout << "\\n";
  }
}

#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)
// }}}

// types {{{
using ll = long long;
using ull = unsigned long long;
// }}}

struct Edge {
  int to;
  int cost;
  Edge(int to_arg, int cost_arg) : to(to_arg), cost(cost_arg) {}
};

constexpr int INF = numeric_limits<int>::max();

int N;
vector<Edge> G[10000];

void solve() {
  vector<int> cost(N);
  deque<bool> done(N);

  fill(begin(cost), end(cost), INF);
  cost[0] = 0;

  for (;;) {
    int mincost = INF;
    int minv = -1;

    REP(v, N) {
      if (done[v])
        continue;
      if (cost[v] < mincost) {
        mincost = cost[v];
        minv = v;
      }
    }

    if (minv == -1)
      break;

    done[minv] = true;

    for (const Edge &e : G[minv]) {
      if (done[e.to])
        continue;
      int cost_new = cost[minv] + e.cost;
      if (cost_new < cost[e.to]) {
        cost[e.to] = cost_new;
      }
    }
  }

  REP(v, N) { cout << v << " " << cost[v] << "\\n"; }
}

int main() {
  cin >> N;
  REP(_, N) {
    int from, k;
    cin >> from >> k;
    REP(_, k) {
      int to, cost;
      cin >> to >> cost;
      G[from].emplace_back(to, cost);
    }
  }

  solve();

  return 0;
}

Error: MLE

Fixed submission:

// #include {{{
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// }}}

using namespace std;

// macros {{{
template <typename T, size_t N> constexpr size_t NELEMS(T (&)[N]) { return N; }

template <typename InputIt> void PRINT_RANGE(InputIt first, InputIt last) {
  for (; first != last; ++first)
    cout << *first << (first == last ? "" : " ");
  cout << "\\n";
}

template <typename T> void PRINT_MATRIX(T mat, size_t nr, size_t nc) {
  for (size_t r = 0; r < nr; ++r) {
    for (size_t c = 0; c < nc; ++c) {
      cout << mat[r][c] << (c == nc - 1 ? "" : " ");
    }
    cout << "\\n";
  }
}

#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)
// }}}

// types {{{
using ll = long long;
using ull = unsigned long long;
// }}}

constexpr int INF = numeric_limits<int>::max();

int N;
int G[10000][10000];

void solve() {
  vector<int> cost(N);
  deque<bool> done(N);

  fill(begin(cost), end(cost), INF);
  cost[0] = 0;

  for (;;) {
    int mincost = INF;
    int minv = -1;

    REP(v, N) {
      if (done[v])
        continue;
      if (cost[v] < mincost) {
        mincost = cost[v];
        minv = v;
      }
    }

    if (minv == -1)
      break;

    done[minv] = true;

    REP(v, N) {
      if (done[v])
        continue;
      if (G[minv][v] == -1)
        continue;
      int cost_new = cost[minv] + G[minv][v];
      if (cost_new < cost[v]) {
        cost[v] = cost_new;
      }
    }
  }

  REP(v, N) { cout << v << " " << cost[v] << "\\n"; }
}

int main() {
  cin >> N;
  REP(from, N) {
    REP(to, N) { G[from][to] = -1; }
  }

  REP(_, N) {
    int from, k;
    cin >> from >> k;
    REP(_, k) {
      int cost, to;
      cin >> to >> cost;
      G[from][to] = cost;
    }
  }

  solve();

  return 0;
}