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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int d[8][8][8][8];
int sx, sy;
int gx, gy;
int dist[10][10];
int n;

#define MAX 10000

int solve(int now, int flag) {
  if (now == n - 1)
    return 0;
  int ret = MAX;
  REP(i, n) if ((flag & 1 << i) == 0) {
    if (dist[now][i] < 6) {
      ret = min(ret, dist[now][i] + solve(i, flag | (1 << i)));
    }
  }
  return ret;
}

int main() {
  int w, h;
  while ((w = getInt()) + (h = getInt())) {
    int b[h][w];
    deque<pair<int, int>> v;

    REP(i, h) REP(j, w) {
      int t = getInt();
      if (t == 2) {
        t = 1;
        sx = j;
        sy = i;
      } else if (t == 3) {
        t = 1;
        gx = j;
        gy = i;
      } else if (t == 4) {
        t = 1;
        v.push_back(mp(j, i));
      }
      b[i][j] = t;
    }

    v.push_back(mp(gx, gy));
    v.push_front(mp(sx, sy));

    REP(i, h) REP(j, w) REP(k, h) REP(l, w) d[i][j][k][l] = MAX;

    REP(i, h) REP(j, w) d[i][j][i][j] = 0;

    REP(i, h) REP(j, w) {
      if (b[i][j] == 0)
        continue;
      if (i < h - 1)
        if (b[i + 1][j] == 1)
          d[i][j][i + 1][j] = d[i + 1][j][i][j] = 1;
      if (j < w - 1)
        if (b[i][j + 1] == 1)
          d[i][j][i][j + 1] = d[i][j + 1][i][j] = 1;
    }

    REP(k, w * h)
    REP(i, w * h) REP(j, w * h) d[i / w][i % w][j / w][j % w] =
        min(d[i / w][i % w][j / w][j % w],
            d[i / w][i % w][k / w][k % w] + d[k / w][k % w][j / w][j % w]);

    // REP(i,h) { REP(j,w) printf("%d
    // ",d[sy][sx][i][j]==10000?-1:d[sy][sx][i][j]); puts(""); }

    n = v.size();

    if (n == 0) {
      if (d[sy][sx][gy][gx] == MAX)
        print(-1);
      else
        print(d[sy][sx][gy][gx]);
      continue;
    }

    REP(i, n) REP(j, n) dist[i][j] = d[v[i].s][v[i].f][v[j].s][v[j].f];

    int ans = solve(0, 0);

    if (ans >= MAX) {
      print(-1);
      continue;
    }

    print(ans);
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while ((n = getInt()) + (m = getInt())) {
    vector<bool> inf(n, false);
    vector<pair<int, pair<int, int>>> data(m);

    inf[0] = true;

    REP(i, m) {
      data[i].f = getInt();
      data[i].s.f = getInt() - 1;
      data[i].s.s = getInt() - 1;
    }

    sort(data.begin(), data.end());

    REP(i, m) {
      int s = data[i].s.f;
      int d = data[i].s.s;

      if (inf[s])
        inf[d] = true;
    }

    int ans = 0;
    REP(i, n) if (inf[i])++ ans;

    print(ans);
  }
  return 0;
}