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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define pb push_back
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
vector<string> s;
int w, h;
vector<P> p;
vector<vector<int>> dist;
map<P, int> mp;
vector<vector<int>> dp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (x >= 0 && x < w && y >= 0 && y < h)
    return true;
  return false;
}
int TSP(int now, int bit) {
  if (bit == (1 << p.size()) - 1) {
    return 0;
  }
  if (dp[bit][now] != -1)
    return dp[bit][now];
  int ret = INF;
  for (int i = 1; i < p.size(); i++) {
    if (!((1 << i) & bit)) {
      ret = min(TSP(i, bit | (1 << i)) + dist[now][i], ret);
    }
  }

  return dp[bit][now] = ret;
}
int main() {
  while (cin >> w >> h, w || h) {

    s.resize(h);
    p.resize(1);
    mp.clear();
    p[0] = P(0, 0);

    for (int i = 0; i < h; i++) {
      cin >> s[i];
      for (int j = 0; j < w; j++) {
        if (s[i][j] == 'o') {
          p[0] = P(i, j);
          mp[P(i, j)] = 0;
        } else if (s[i][j] == '*') {
          mp[P(i, j)] = p.size();
          p.pb(P(i, j));
        }
      }
    }
    dist = vector<vector<int>>(p.size(), vector<int>(p.size(), INF));
    bool flag = true;
    for (int i = 0; i < p.size(); i++) {
      queue<P> que;
      vector<vector<int>> d(h, vector<int>(w, INF));
      vector<vector<bool>> used(h, vector<bool>(w, false));
      d[p[i].first][p[i].second] = 0;
      que.push(p[i]);
      int cnt = 0;
      while (!que.empty()) {
        P t = que.front();
        que.pop();
        if (used[t.first][t.second]) {
          continue;
        }
        if (find(all(p), t) != p.end() && dist[i][mp[t]] == INF) {
          dist[i][mp[t]] = d[t.first][t.second];
          cnt++;
        }
        used[t.first][t.second] = true;
        for (int i = 0; i < 4; i++) {
          int nx = dx[i] + t.second, ny = dy[i] + t.first;
          if (inf(ny, nx) && !used[ny][nx] && d[ny][nx] == INF &&
              s[ny][nx] != 'x') {
            que.push(P(ny, nx));
            d[ny][nx] = d[t.first][t.second] + 1;
          }
        }
      }
      if (cnt != p.size()) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      cout << -1 << endl;
      continue;
    }

    dp = vector<vector<int>>((1 << p.size()), vector<int>(p.size(), -1));
    cout << TSP(0, 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define pb push_back
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
vector<string> s;
int w, h;
vector<P> p;
vector<vector<int>> dist;
map<P, int> mp;
vector<int> dp;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool inf(int y, int x) {
  if (x >= 0 && x < w && y >= 0 && y < h)
    return true;
  return false;
}
int TSP(int now, int bit) {
  if (bit == (1 << p.size()) - 1) {
    return 0;
  }
  if (dp[bit] != -1)
    return dp[bit];
  int ret = INF;
  for (int i = 1; i < p.size(); i++) {
    if (!((1 << i) & bit)) {
      ret = min(TSP(i, bit | (1 << i)) + dist[now][i], ret);
    }
  }

  return dp[bit] = ret;
}
int main() {
  while (cin >> w >> h, w || h) {
    dist = vector<vector<int>>(h, vector<int>(w, INF));
    s.resize(h);
    p.resize(1);
    mp.clear();
    p[0] = P(0, 0);

    for (int i = 0; i < h; i++) {
      cin >> s[i];
      for (int j = 0; j < w; j++) {
        if (s[i][j] == 'o') {
          p[0] = P(i, j);
          mp[P(i, j)] = 0;
        } else if (s[i][j] == '*') {
          mp[P(i, j)] = p.size();
          p.pb(P(i, j));
        }
      }
    }
    bool flag = true;
    for (int i = 0; i < p.size(); i++) {
      queue<P> que;
      vector<vector<int>> d(h, vector<int>(w, INF));
      d[p[i].first][p[i].second] = 0;
      que.push(p[i]);
      int cnt = 0;
      while (!que.empty()) {
        P t = que.front();
        que.pop();
        if (find(all(p), t) != p.end() && dist[i][mp[t]] == INF) {
          dist[i][mp[t]] = d[t.first][t.second];
          cnt++;
        }
        for (int i = 0; i < 4; i++) {
          int nx = dx[i] + t.second, ny = dy[i] + t.first;
          if (inf(ny, nx) && d[ny][nx] == INF && s[ny][nx] != 'x') {
            que.push(P(ny, nx));
            d[ny][nx] = d[t.first][t.second] + 1;
          }
        }
      }
      if (cnt != p.size()) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      cout << -1 << endl;
      continue;
    }
    dp = vector<int>((1 << p.size()), -1);
    cout << TSP(0, 1) << endl;
  }
  return 0;
}