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

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned short ushort;

class blanch {
public:
  bool visited;
  int distance;
  ushort from;
  ushort to;
};
typedef vector<blanch> vb;
typedef vector<vb> vvb;

int make_graph(vvb &graph, vb &nodes) {

  int n = nodes.size();
  for (ushort i = 0; i < n; i++) {
    nodes[i].visited = false;
    nodes[i].distance = INT_MAX;
  }

  for (ushort i = 0; i < n; i++) {
    int i2, n2;
    cin >> i2;
    cin >> n2;

    blanch bl0;
    bl0.visited = false;
    bl0.from = i2;

    for (ushort j = 0; j < n2; j++) {
      ushort j2;
      cin >> bl0.to;
      cin >> bl0.distance;

      graph[i2].push_back(bl0);
    }
  }
  return 0;
}

int Shortest_Path(vvb &graph, vb &nodes) {

  int n = graph.size(), p1 = 0;
  nodes[p1].distance = 0;
  int p2;
  while (p1 != -1) {
    nodes[p1].visited = true;
    for (int i = 0; i < (int)graph[p1].size(); i++) {
      blanch tmp = graph[p1][i];
      p2 = tmp.to;
      if (nodes[p2].visited == false) {
        int tmp2 = tmp.distance + nodes[p1].distance;
        if (tmp2 < nodes[p2].distance) {
          tmp.distance = tmp2;
          nodes[p2] = tmp;
        }
      }
    }
    p1 = -1;
    int tmp;
    tmp = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (nodes[i].visited == false && nodes[i].distance < tmp) {
        p1 = i;
        tmp = nodes[i].distance;
      }
    }
  }
  return 0;
}

int resultout(vb &nodes) {
  int n = nodes.size();
  for (int i = 0; i < n; i++) {
    cout << i << " " << nodes[i].distance << endl;
  }
  return 0;
}

int main(void) {

  int n;
  cin >> n;
  vvb G(n, vb());
  vb nodes(n);

  make_graph(G, nodes);

  Shortest_Path(G, nodes);

  resultout(nodes);
  G.clear();
  nodes.clear();

  return 0;
}

Error: MLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned short ushort;
typedef vector<int> vi;
typedef vector<vi> vvi;

class blanch {
public:
  bool visited;
  int distance;
  ushort from;
};
typedef vector<blanch> vb;

int make_graph(vvi &graph, vb &nodes) {
  int n = graph.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      graph[i][j] = -1;
    }
    nodes[i].visited = false;
    nodes[i].distance = INT_MAX;
  }

  for (int i = 0; i < n; i++) {
    int i2, n2;
    cin >> i2;
    cin >> n2;
    for (int j = 0; j < n2; j++) {
      int j2;
      cin >> j2;
      cin >> graph[i2][j2];
    }
  }
  return 0;
}

int Shortest_Path(vvi &graph, vb &nodes) {

  int n = graph.size(), p1 = 0;
  nodes[p1].distance = 0;

  while (p1 != -1) {
    nodes[p1].visited = true;
    for (int p2 = 0; p2 < n; p2++) {
      int *tmp = &graph[p1][p2];
      if (*tmp != -1 && nodes[p2].visited == false) {
        int tmp2 = *tmp + nodes[p1].distance;
        if (tmp2 < nodes[p2].distance) {
          nodes[p2].distance = tmp2;
          nodes[p2].from = p1;
        }
      }
    }

    p1 = -1;
    int tmp;
    tmp = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (nodes[i].visited == false && nodes[i].distance < tmp) {
        p1 = i;
        tmp = nodes[i].distance;
      }
    }
  }
  return 0;
}

int resultout(vb &nodes) {
  int n = nodes.size();
  for (int i = 0; i < n; i++) {
    cout << i << " " << nodes[i].distance << endl;
  }
  return 0;
}

int main(void) {

  int n;
  cin >> n;
  vvi G(n, vi(n));
  vb nodes(n);

  make_graph(G, nodes);

  Shortest_Path(G, nodes);

  resultout(nodes);

  return 0;
}