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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
int const inf = 1 << 29;

int main() {
  int w, h;
  while (cin >> w >> h && w | h) {
    vector<string> grid(h);
    vector<pair<int, int>> node;
    rep(i, h) {
      cin >> grid[i];
      rep(j, grid[i].size()) if (grid[i][j] == 'o') node.emplace_back(i, j);
    }
    rep(i, h) rep(j, w) {
      if (grid[i][j] == '*')
        node.emplace_back(i, j);
    }

    static int d[20][20];
    rep(i, 20) rep(j, 20) d[i][j] = inf;

    rep(i, node.size()) {
      int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
      queue<pair<int, int>> q;
      static int dist[25][25];
      rep(j, h) rep(k, w) dist[j][k] = inf;
      dist[node[i].first][node[i].second] = 0;
      q.emplace(node[i]);

      while (q.size()) {
        auto p = q.front();
        q.pop();
        int cx = p.second, cy = p.first;
        rep(j, 4) {
          int nx = cx + dx[j], ny = cy + dy[j];
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;
          if (grid[ny][nx] == 'x')
            continue;
          if (dist[ny][nx] > dist[cy][cx] + 1) {
            dist[ny][nx] = dist[cy][cx] + 1;
            q.emplace(ny, nx);
          }
        }
      }

      rep(j, node.size()) { d[i][j] = dist[node[j].first][node[j].second]; }
    }

    int idx[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    long long ans = inf;
    do {
      long long t = 0;
      rep(i, node.size() - 1) { t += d[idx[i]][idx[i + 1]]; }
      ans = min(t, ans);
    } while (next_permutation(idx + 1, idx + node.size()));
    if (ans >= inf)
      ans = -1;
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int const inf = 1 << 29;
typedef int Weight;
struct Edge {
  Edge(int src, int dst, int cost) : src(src), dst(dst), cost(cost) {}
  int src, dst, cost;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int W, H;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

// テ」ツ?づ」ツつ甘ヲツ慊ャテ」ツ?ョテ」ツδ?」ツつ、テ」ツつッテ」ツつケテ」ツδ暗」ツδゥテヲツウツ陛」ツつ担pagetti
// Sourceテ」ツ?ョAPIテ」ツ?ォテ・ツ青暗」ツつ湘」ツ?崚」ツ?淌」ツつづ」ツ?ョ
struct State {
  int v, d;
  State(int v, int d) : v(v), d(d) {}
  bool operator<(const State &s) const { return d > s.d; }
};

void dijkstra(Graph const &g, int s, vector<Weight> &dist) {
  priority_queue<State> q;
  dist.assign(g.size(), inf);
  dist[s] = 0;
  q.push(State(s, 0));
  while (q.size()) {
    State s = q.top();
    q.pop();
    int v = s.v;
    if (dist[v] < s.d)
      continue;
    rep(i, g[v].size()) {
      const Edge &e = g[v][i];
      if (dist[e.dst] > dist[v] + e.cost) {
        dist[e.dst] = dist[v] + e.cost;
        q.push(State(e.dst, dist[e.dst]));
      }
    }
  }
}

inline bool valid(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

int solve(vector<string> grid) {
  Graph g(W * H);
  vector<int> dust;
  int s;
  rep(y, H) rep(x, W) {
    rep(i, 4) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      if (!valid(tx, ty))
        continue;
      if (grid[ty][tx] == 'x')
        continue;
      int src = y * H + x;
      int dst = ty * H + tx;
      g[src].push_back(Edge(src, dst, 1));
    }
  }

  rep(i, H) rep(j, W) {
    if (grid[i][j] == '*') {
      dust.push_back(i * H + j);
    }
    if (grid[i][j] == 'o') {
      s = i * H + j;
    }
  }
  dust.push_back(s);
  int V = dust.size();

  vector<vector<int>> d(V, vector<int>(V, inf));

  rep(i, V) { dijkstra(g, dust[i], d[i]); }

  int idx[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
  int ans = inf;
  do {
    long long res = 0;
    rep(i, V) { res += d[idx[i]][idx[i + 1]]; }
    if (res >= inf)
      continue;
    ans = min<long long>(ans, res);
  } while (next_permutation(idx, idx + V - 1));
  if (ans == inf)
    ans = -1;
  return ans;
}

int main() {
  while (cin >> W >> H && W | H) {
    vector<string> grid(H);
    rep(i, H) { cin >> grid[i]; }
    cout << solve(grid) << endl;
  }
}