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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 10000;

struct edge {
  int to, cost;
  edge(int _to, int _cost) {
    to = _to;
    cost = _cost;
  }
};

int V;
vector<edge> cost[MAX_N];
int d[MAX_N];
bool visit[MAX_N];
void dijkstra(int s) {
  d[s] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> que;
  que.push(mp(0, s));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < cost[v].size(); i++) {
      edge e = cost[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(mp(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V;
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int to, c;
      cin >> to >> c;
      cost[u].push_back(edge(to, c));
    }
  }

  dijkstra(0);
  for (int i = 0; i < V; i++) {
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 10000;
int V;
int cost[MAX_N][MAX_N];
int d[MAX_N];
bool visit[MAX_N];
void dijkstra(int s) {
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!visit[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1)
      break;
    visit[v] = true;
    for (int u = 0; u < V; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

int main() {
  cin >> V;
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    visit[i] = false;
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < V; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;
      cost[u][v] = c;
    }
  }
  dijkstra(0);
  for (int i = 0; i < V; i++) {
    cout << i << " " << d[i] << endl;
  }
  return 0;
}