alexjercan/bugnet huggingface dataset:
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>

using namespace std;

struct Node {
  int id;
  int cost;
  bool done;
  vector<int> next_node;
  vector<int> len_cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[10000];

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    node[id].id = id;
    node[id].cost = INT_MAX;
    node[id].done = false;
    node[id].next_node.resize(k);
    node[id].len_cost.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> node[id].next_node[j] >> node[id].len_cost[j];
    }
  }

  priority_queue<Node> q;
  node[0].cost = 0;
  q.push(node[0]);
  while (!q.empty()) {
    Node temp = q.top();
    q.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    for (int i = 0; i < temp.next_node.size(); i++) {
      int to = temp.next_node[i];
      int cost = temp.cost + temp.len_cost[i];
      if (node[to].cost > cost) {
        node[to].cost = cost;
        if (node[to].done == false) {
          q.push(node[to]);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << node[i].cost << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>

using namespace std;

struct Node {
  int id;
  int cost;
  bool done;
  vector<int> next_node;
  vector<int> len_cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[100];

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    node[id].id = id;
    node[id].cost = INT_MAX;
    node[id].done = false;
    node[id].next_node.resize(k);
    node[id].len_cost.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> node[id].next_node[j] >> node[id].len_cost[j];
    }
  }

  priority_queue<Node> q;
  node[0].cost = 0;
  q.push(node[0]);
  while (!q.empty()) {
    Node temp = q.top();
    q.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    for (int i = 0; i < temp.next_node.size(); i++) {
      int to = temp.next_node[i];
      int cost = temp.cost + temp.len_cost[i];
      if (node[to].cost > cost) {
        node[to].cost = cost;
        if (node[to].done == false) {
          q.push(node[to]);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << node[i].cost << endl;
  }

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>

using namespace std;

struct Node {
  int id;
  int cost;
  bool done;
  vector<int> next_node;
  vector<int> len_cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[10000];

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    node[id].id = id;
    node[id].cost = INT_MAX;
    node[id].done = false;
    node[id].next_node.resize(k);
    node[id].len_cost.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> node[id].next_node[j] >> node[id].len_cost[j];
    }
  }

  priority_queue<Node> q;
  node[0].cost = 0;
  q.push(node[0]);
  while (!q.empty()) {
    Node temp = q.top();
    q.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    for (int i = 0; i < temp.next_node.size(); i++) {
      int to = temp.next_node[i];
      int cost = temp.cost + temp.len_cost[i];
      if (node[to].cost > cost) {
        node[to].cost = cost;
        q.push(node[to]);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << node[i].cost << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> adja[10000];

#define INF (1 << 30)

int main() {
  int n, id, k, a, b;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    for (int j = 0; j < k; ++j) {
      cin >> a >> b;
      adja[id].push_back(make_pair(a, b));
    }
  }

  priority_queue<pair<int, int>> que;
  que.push(make_pair(0, 0)); // cost, node
  int cost[10000];
  fill(cost, cost + n, INF);
  cost[0] = 0;

  while (!que.empty()) {
    pair<int, int> top = que.top();
    que.pop();

    for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
      if (cost[adja[top.second][j].first] >
          top.first + adja[top.second][j].second) {
        cost[adja[top.second][j].first] =
            top.first + adja[top.second][j].second;
        que.push(make_pair(top.first + adja[top.second][j].second,
                           adja[top.second][j].first));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << cost[i] << endl;
  }

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

#define MAX 10000
#define INFTY 99999999

#define WHITE 0
#define GRAY 1
#define BLACK 2

struct Node {
  vector<int> ch;
  vector<int> co;
};

Node G[MAX];

int n;
int d[MAX];
int p[MAX];
int color[MAX];

void dijkstra(int s) {
  int u;

  for (int i = 0; i < n; i++) {
    p[i] = -1;
    d[i] = INFTY;
  }

  d[s] = 0;

  while (true) {

    int mincost = INFTY;

    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY) {
      break;
    }

    color[u] = BLACK;

    for (int i = 0; i < G[u].ch.size(); i++) {
      int co = G[u].co[i];
      int ch = G[u].ch[i];

      if (color[ch] != BLACK) {
        if (d[u] + co < d[ch]) {
          d[ch] = d[u] + co;
          p[ch] = d[u];
          color[ch] = GRAY;
        }
      }
    }
  }
}

int main(void) {
  int u;
  int k;
  int v;
  int c;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].ch.push_back(v);
      G[u].co.push_back(c);
    }
  }

  dijkstra(0);

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define MAX 10000
#define INFTY 99999999

#define WHITE 0
#define GRAY 1
#define BLACK 2

struct Node {
  vector<int> ch;
  vector<int> co;
};

Node G[MAX];

int n;
int d[MAX];
int p[MAX];
int color[MAX];

void dijkstra(int s) {
  int u;

  for (int i = 0; i < n; i++) {
    p[i] = -1;
    d[i] = INFTY;
  }

  d[s] = 0;

  while (true) {

    int mincost = INFTY;

    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY) {
      break;
    }

    color[u] = BLACK;

    for (int i = 0; i < G[u].ch.size(); i++) {
      if (color[G[u].ch[i]] != BLACK) {
        if (d[u] + G[u].co[i] < d[G[u].ch[i]]) {
          d[G[u].co[i]] = d[u] + G[u].co[i];
          p[G[u].co[i]] = d[u];
          color[G[u].co[i]] = GRAY;
        }
      }
    }
  }
}

int main(void) {
  int u;
  int k;
  int v;
  int c;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].ch.push_back(v);
      G[u].co.push_back(c);
    }
  }

  dijkstra(0);

  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int inf = 1e9;
// ダイクストラO(elogv)
struct edge {
  int to, cost;
  edge(int t, int c) { to = t, cost = c; }
};
typedef pair<int, int> P;
int N;
vector<edge> G[11111];
int d[11111];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  FOR(i, 1, N + 1) d[i] = inf;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  FOR(i, 0, N) {
    int u, k;
    cin >> u >> k;
    FOR(j, 0, k) {
      int v, c;
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  dijkstra(0);
  FOR(i, 0, N) { cout << i << " " << d[i] << endl; }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int inf = 1e9;
// ダイクストラO(elogv)
struct edge {
  int to, cost;
  edge(int t, int c) { to = t, cost = c; }
};
typedef pair<int, int> P;
int N;
vector<edge> G[111];
int d[111];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  FOR(i, 1, N + 1) d[i] = inf;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  FOR(i, 0, N) {
    int u, k;
    cin >> u >> k;
    FOR(j, 0, k) {
      int v, c;
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  dijkstra(0);
  FOR(i, 0, N) { cout << i << " " << d[i] << endl; }
}
Problem p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>

#define INF 1000000000
using namespace std;

struct edge {
  int from;
  int to;
  int w;
};
struct node {
  node() : id{-1}, d{INF}, ns{} {};
  node(int i, vector<edge> &vec) : id{i}, d{INF}, ns{vec} {};
  int id;
  int d;
  vector<edge> ns;
};

struct node_s {
  int id;
  int d;
};

vector<int> dijkstra(node *graph, int n, int st) {
  auto ds = vector<int>(n, INF);
  auto comp = [](node_s *x, node_s *y) { return x->d > y->d; };
  priority_queue<node_s *, vector<node_s *>, decltype(comp)> que(comp);
  node_s *start = new node_s{graph[st].id, 0};
  que.push(start);
  while (que.size()) {
    node_s *n = que.top();
    //    cout << n->id << ' ' << n->d << endl;
    que.pop();
    if (n->d < ds[n->id]) {
      ds[n->id] = n->d;
      for (auto e : graph[n->id].ns) {
        if (ds[e.to] < INF) {
          continue;
        }
        node_s *next_n = new node_s{graph[e.to].id, ds[n->id] + e.w};
        que.push(next_n);
      }
    }
    delete n;
  }
  return ds;
}

int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int n;
  cin >> n;
  node graph[n];
  for (int i = 0; i < n; ++i) {
    int id;
    int m;
    cin >> id >> m;
    auto vec = vector<edge>(m);
    int v, c;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      vec[j] = edge{id, v, c};
    }
    graph[id] = node(id, vec);
  }
  auto ds = dijkstra(graph, n, 0);
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << ds[i] << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

#define INF 1000000000
using namespace std;

struct edge {
  int from;
  int to;
  int w;
};
struct node {
  node() : id{-1}, d{INF}, ns{} {};
  node(int i, vector<edge> &vec) : id{i}, d{INF}, ns{vec} {};
  int id;
  int d;
  vector<edge> ns;
};

vector<int> dijkstra(node *graph, int n, int st) {
  auto ds = vector<int>(n, INF);
  auto comp = [](node *x, node *y) { return x->d < y->d; };
  priority_queue<node *, vector<node *>, decltype(comp)> que(comp);
  node *start = &graph[st];
  start->d = 0;
  que.push(start);
  while (que.size()) {
    node *n = que.top();
    que.pop();
    if (n->d < ds[n->id]) {
      ds[n->id] = n->d;
      for (auto e : n->ns) {
        node *next_n = &graph[e.to];
        next_n->d = min(next_n->d, n->d + e.w);
        que.push(next_n);
      }
    }
  }
  return ds;
}

int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int n;
  cin >> n;
  node graph[n];
  for (int i = 0; i < n; ++i) {
    int id;
    int m;
    cin >> id >> m;
    auto vec = vector<edge>(m);
    int v, c;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      vec[j] = edge{id, v, c};
    }
    graph[id] = node(id, vec);
  }
  auto ds = dijkstra(graph, n, 0);
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << ds[i] << endl;
  }
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

vector<int> adja[10000], cost[10000];

#define INF (1 << 30)

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    adja[id].resize(k);
    cost[id].resize(k);
    for (int j = 0; j < k; ++j) {
      cin >> adja[id][j] >> cost[id][j];
    }
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0)); // cost, node
  int best[10000];
  fill(best, best + n, INF);
  best[0] = 0;

  while (!que.empty()) {
    pair<int, int> top = que.top();
    que.pop();

    if (best[top.second] < top.first) {
      continue;
    }

    for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
      if (best[adja[top.second][j]] > top.first + cost[top.second][j]) {
        best[adja[top.second][j]] = top.first + cost[top.second][j];
        que.push(
            make_pair(top.first + cost[top.second][j], adja[top.second][j]));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << best[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> adja[10000], cost[10000];

#define INF (1 << 30)

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    adja[id].resize(k);
    cost[id].resize(k);
    for (int j = 0; j < k; ++j) {
      cin >> adja[id][j] >> cost[id][j];
    }
  }

  priority_queue<pair<int, int>> que;
  que.push(make_pair(0, 0)); // cost, node
  int best[10000];
  fill(best, best + n, INF);
  best[0] = 0;

  while (!que.empty()) {
    pair<int, int> top = que.top();
    que.pop();

    if (best[top.second] < top.first) {
      continue;
    }

    for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
      if (best[adja[top.second][j]] > top.first + cost[top.second][j]) {
        best[adja[top.second][j]] = top.first + cost[top.second][j];
        que.push(
            make_pair(top.first + cost[top.second][j], adja[top.second][j]));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << best[i] << endl;
  }

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define MAX 10000
#define INF 1000000
#define pii pair<int, int>

vector<pii> eclist[MAX]; //[[index,cost]]

void bfs(int start, int n) {
  queue<int> q; // index
  vector<bool> visited(n, false);
  q.push(start);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visited[u] = true;
    for (pii vpair : eclist[u]) {
      int v = vpair.first;
      if (!visited[v]) {
        q.push(v);
      }
    }
  }
}

void dijkstra_print_distance(int start, int n) {
  priority_queue<pii> q; // cost,index
  vector<int> costs(n, INF);
  vector<bool> visited(n, false);
  costs[0] = 0;
  q.push(make_pair(0, start));
  while (!q.empty()) {
    pii upair = q.top();
    q.pop();
    int u = upair.second, ucost = upair.first * -1;
    visited[u] = true;
    if (costs[u] < ucost) {
      continue;
    }
    for (pii vpair : eclist[u]) {
      int v = vpair.first, vcost = vpair.second;
      if (visited[v])
        continue;
      if (costs[v] > costs[u] + vcost) {
        costs[v] = costs[u] + vcost;
        q.push(pii(costs[v] * -1, v));
      }
    }
  }
  for (int i = 0; i < n; ++i)
    cout << i << " " << costs[i] << endl;
}

int main() {
  // freopen("in", "r", stdin);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int id, m, v, c;
    cin >> id >> m;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      eclist[id].push_back(pair<int, int>(v, c));
    }
  }

  dijkstra_print_distance(0, n);

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define MAX 10000
#define INF 1000000
#define pii pair<int, int>

vector<pii> eclist[MAX]; //[[index,cost]]

void bfs(int start, int n) {
  queue<int> q; // index
  vector<bool> visited(n, false);
  q.push(start);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visited[u] = true;
    for (pii vpair : eclist[u]) {
      int v = vpair.first;
      if (!visited[v]) {
        q.push(v);
      }
    }
  }
}

void dijkstra_print_distance(int start, int n) {
  priority_queue<pii> q; // cost,index
  vector<int> costs(n, INF);
  vector<bool> visited(n, false);
  costs[0] = 0;
  q.push(make_pair(0, start));
  while (!q.empty()) {
    pii upair = q.top();
    q.pop();
    int u = upair.second, ucost = upair.first * -1;
    visited[u] = true;
    if (costs[u] < ucost) {
      continue;
    }
    for (pii vpair : eclist[u]) {
      int v = vpair.first, vcost = vpair.second;
      if (visited[v])
        continue;
      if (costs[v] > costs[u] + vcost) {
        costs[v] = costs[u] + vcost;
        q.push(pii(costs[v] * -1, v));
      }
    }
  }
  for (int i = 0; i < n; ++i)
    cout << i << " " << costs[i] << endl;
}

int main() {
  int n;
  freopen("in", "r", stdin);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int id, m, v, c;
    cin >> id >> m;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      eclist[id].push_back(pair<int, int>(v, c));
    }
  }

  dijkstra_print_distance(0, n);

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 10005
#define INFTY INT_MAX
int n;

struct NW {
  int point, weight;
};

vector<vector<NW>> ve;
vector<NW> vNW;
NW nw;

void dijkstra() {
  int color[MAX], d[MAX];
  for (int i = 0; i < n; i++) {
    color[i] = 0;
    d[i] = INFTY;
  }
  d[0] = 0;
  while (1) {
    int u, minv = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != 1 && d[i] < minv) {
        u = i;
        minv = d[i];
      }
    }
    if (minv == INFTY)
      break;
    color[u] = 1;
    for (int i = 0; i < ve[u].size(); i++) {
      int v = ve[u][i].point;
      int c = ve[u][i].weight;
      if (color[v] != 1 && d[u] + c < d[v]) {
        d[v] = d[u] + c;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

int main(void) {
  cin >> n;
  ve.resize(n);
  int u, k;
  for (int i = 0; i < n; i++) {
    vNW.clear();
    cin >> u >> k;
    while (k--) {
      cin >> nw.point >> nw.weight;
      vNW.push_back(nw);
    }
    ve[u] = vNW;
  }

  dijkstra();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY INT_MAX

int n, G[MAX][MAX];

void dijkstra() {
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = 0;
  }
  d[0] = 0;
  while (1) {
    int u = -1;
    int minv = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != 1 && d[i] < minv) {
        minv = d[i];
        u = i;
      }
    }
    if (minv == INFTY)
      break;
    color[u] = 1;
    for (int v = 0; v < n; v++) {
      if (color[v] != 1 && G[u][v] != INFTY) {
        if (d[u] + G[u][v] < d[v])
          d[v] = d[u] + G[u][v];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[i][j] = INFTY;
    }
  }
  int k, v, c;
  for (int i = 0; i < n; i++) {
    cin >> k >> k;
    while (k--) {
      cin >> v >> c;
      G[i][v] = c;
    }
  }
  dijkstra();
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

struct edge {
  int to, cost;
};

// pair<int,int> ??????first??????????????¢,second??????????????????

int v;
vector<edge> g[10000];
int d[10000];
const int INF = 10000000;

// s?????????????????????!
void dijkstra(int s) {

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  fill(d, d + v, INF);
  d[s] = 0;
  que.push(pair<int, int>(0, s));

  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int k = p.second;
    if (d[k] < p.first)
      continue;

    for (int i = 0; i < g[k].size(); i++) {
      edge e = g[k][i];
      if (d[e.to] > d[k] + e.cost) {
        d[e.to] = d[k] + e.cost;
        que.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }

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

int main() {

  cin >> v;

  int u, k, b, c;

  for (int i = 0; i < v; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> b >> c;
      edge ed;
      ed.to = b;
      ed.cost = c;
      g[i].push_back(ed);
    }
  }

  dijkstra(0);

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int cost[10000][10000]; // cost[a][b] a??????b????§?????????????????????????
int d[10000];           //??????s?????????????°??????¢
bool used[10000];
int arrow; //????????°

const int INF = 10000000;

void dijkstra() {
  fill(d, d + arrow, INF);
  fill(used, used + arrow, false);
  d[0] = 0;

  while (1) {
    int v = -1;
    // ???????????????????????????????????????????????¢???????°???????????????¢???
    for (int u = 0; u < arrow; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }

    if (v == -1)
      break;

    used[v] = true;

    for (int u = 0; u < arrow; u++) {
      if (cost[v][u] != INF) {
        d[u] = min(d[u], d[v] + cost[v][u]);
      }
    }
  }

  for (int u = 0; u < arrow; u++) {
    cout << u << " " << d[u] << endl;
  }
}

int main() {

  cin >> arrow;

  int n, a, v, c;

  for (int i = 0; i < arrow; i++) {
    for (int j = 0; j < arrow; j++) {
      cost[i][j] = INF;
    }
  }

  for (int u = 0; u < arrow; u++) {

    cin >> n >> a;

    for (int i = 0; i < a; i++) {
      cin >> v >> c;
      cost[n][v] = c;
    }
  }

  dijkstra();

  return 0;
}