Problem p00072 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

class node {
public:
  int num;
};

class edge {
public:
  node b, e;
  int cost;
};

class graph {
public:
  vector<node> nodes;
  vector<edge> edges;
  int n;
  int e;

  void set(void) {
    nodes.clear();
    edges.clear();
    n = 0;
    e = 0;
  }

  void Nadd(node a) {
    if (n == 0) {
      nodes.push_back(a);
      n++;
    } else {
      int i = 0;
      while (nodes[i].num < a.num && i < n)
        i++;
      nodes.insert(nodes.begin() + i, a);
      n++;
    }
  }

  void Eadd(edge a) {
    if (e == 0) {
      edges.push_back(a);
      e++;
    } else {
      int i = 0;
      while (edges[i].cost < a.cost && i < e)
        i++;
      edges.insert(edges.begin() + i, a);
      e++;
    }
  }

  bool Nexist(node a) {
    for (int i = 0; i < n; i++) {
      if (a.num == nodes[i].num)
        return true;
    }
    return false;
  }

  graph prim(void) {
    graph res;

    res.set();

    if (!e)
      return res;
    else {
      res.Eadd(edges[0]);
      res.Nadd(edges[0].b);
      res.Nadd(edges[0].e);

      while (res.n < n) {
        for (int i = 0; i < e; i++) {
          if (res.Nexist(edges[i].b) && !res.Nexist(edges[i].e)) {
            res.Eadd(edges[i]);
            res.Nadd(edges[i].e);
            i = 0;
          } else if (!res.Nexist(edges[i].b) && res.Nexist(edges[i].e)) {
            res.Eadd(edges[i]);
            res.Nadd(edges[i].b);
            i = 0;
          }
        }
      }
      return res;
    }
  }
};

int main() {
  int n, m;
  int ans;
  graph map, prim;
  node tmp;
  edge tmp2;

  while (1) {
    cin >> n;
    if (!n)
      break;

    map.set();
    for (int i = 0; i < n; i++) {
      tmp.num = i;
      map.Nadd(tmp);
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      scanf("%d,%d,%d", &tmp2.b.num, &tmp2.e.num, &tmp2.cost);
      map.Eadd(tmp2);
    }

    prim = map.prim();

    ans = 0;
    for (int i = 0; i < prim.e; i++)
      ans += prim.edges[i].cost - 100;

    cout << ans / 100 << endl;
  }
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

class node {
public:
  int num;
};

class edge {
public:
  node b, e;
  int cost;
};

class graph {
public:
  vector<node> nodes;
  vector<edge> edges;
  int n;
  int e;

  void set(void) {
    nodes.clear();
    edges.clear();
    n = 0;
    e = 0;
  }

  void Nadd(node a) {
    if (n == 0) {
      nodes.push_back(a);
      n++;
    } else {
      int i = 0;
      while (nodes[i].num < a.num && i < n)
        i++;
      nodes.insert(nodes.begin() + i, a);
      n++;
    }
  }

  void Eadd(edge a) {
    if (e == 0) {
      edges.push_back(a);
      e++;
    } else {
      int i = 0;
      while (edges[i].cost < a.cost && i < e)
        i++;
      edges.insert(edges.begin() + i, a);
      e++;
    }
  }

  bool Nexist(node a) {
    for (int i = 0; i < n; i++) {
      if (a.num == nodes[i].num)
        return true;
    }
    return false;
  }

  graph prim(void) {
    graph res;
    graph tmp;
    tmp.set();
    for (int i = 0; i < e; i++)
      tmp.Eadd(edges[i]);

    res.set();

    if (!e)
      return res;
    else {
      res.Eadd(tmp.edges[0]);
      res.Nadd(tmp.edges[0].b);
      res.Nadd(tmp.edges[0].e);
      edges.erase(tmp.edges.begin());
      tmp.e--;

      while (res.n < n) {
        for (int i = 0; i < tmp.e; i++) {
          if (res.Nexist(tmp.edges[i].b) && !res.Nexist(tmp.edges[i].e)) {
            res.Eadd(tmp.edges[i]);
            res.Nadd(tmp.edges[i].e);
            tmp.edges.erase(tmp.edges.begin() + i);
            tmp.e--;
            i = 0;
          } else if (!res.Nexist(tmp.edges[i].b) &&
                     res.Nexist(tmp.edges[i].e)) {
            res.Eadd(tmp.edges[i]);
            res.Nadd(tmp.edges[i].b);
            tmp.edges.erase(tmp.edges.begin() + i);
            tmp.e--;
            i = 0;
          }
        }
      }
      return res;
    }

    return tmp;
  }
};

int main() {
  int n, m;
  int ans;
  graph map, prim;
  node tmp;
  edge tmp2;

  while (1) {
    cin >> n;
    if (!n)
      break;

    map.set();
    for (int i = 0; i < n; i++) {
      tmp.num = i;
      map.Nadd(tmp);
    }
    map.n = n;

    cin >> m;
    for (int i = 0; i < m; i++) {
      scanf("%d,%d,%d", &tmp2.b.num, &tmp2.e.num, &tmp2.cost);
      map.Eadd(tmp2);
    }
    map.e = m;

    prim = map.prim();

    ans = 0;
    for (int i = 0; i < prim.e; i++)
      ans += prim.edges[i].cost - 100;

    cout << ans / 100 << endl;
  }
  return 0;
}