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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main() {
  const int INFTY = 1 << 29;
  for (int n, m; cin >> n >> m, n | m;) {
    vvi a(n, vi(n, INFTY));
    rep(i, m) {
      int s, d;
      cin >> s >> d;
      a[s - 1][d - 1] = a[d - 1][s - 1] = 1;
    }

    vi dist(n, INFTY), visited(n, 0);
    dist[0] = 0;
    rep(i, n) {
      int minval = INFTY, minid = -1;
      rep(j, n) if (!visited[j] && dist[j] < minval) {
        minval = dist[j];
        minid = j;
      }
      if (minval == INFTY)
        break;
      visited[minid] = 1;
      rep(j, n) dist[j] = min(dist[j], dist[minid] + a[minid][j]);
    }

    int res = 0;
    repi(i, 1, n) res += dist[i] <= 2;

    cout << res << endl;
  }

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main() {
  const int INFTY = 1 << 29;
  for (int n, m; cin >> n >> m, n | m;) {
    vvi a(n, vi(n, INFTY));
    rep(i, m) {
      int s, d;
      cin >> s >> d;
      a[s - 1][d - 1] = a[d - 1][s - 1] = 1;
    }

    vi dist(n, INFTY), visited(n, 0);
    dist[0] = 0;
    rep(i, n) {
      int minval = INFTY, minid = -1;
      rep(j, n) if (!visited[i] && dist[i] < minval) {
        minval = dist[i];
        minid = i;
      }
      visited[minid] = 1;
      rep(j, n) dist[j] = min(dist[j], dist[i] + a[i][j]);
    }

    int res = 0;
    repi(i, 1, n) res += dist[i] <= 2;

    cout << res << endl;
  }

  return 0;
}