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

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
#define INF 999999999
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (n); i++)
vector<vector<char>> inp;
int w, h;

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

struct st {
  pii p;
  int bef;
  int c;
  bool operator<(const st &a) const { return this->c > a.c; }
};
int cost[65][65][2];

#define y first
#define x second
int solve(vector<pii> poses) {
  fill(**cost, **cost + 65 * 65 * 2, INF);
  int ret = INF;
  priority_queue<st> que;
  for (int i = 0; i < poses.size(); i++) {
    pii pos = poses[i];
    que.push(st{pos, 0, 0});
    que.push(st{pos, 1, 0});
    cost[pos.y][pos.x][0] = 0;
    cost[pos.y][pos.x][1] = 0;
  }
  while (que.size()) {
    st q = que.top();
    que.pop();
    int val, add;
    val = q.c;
    rep(i, 9) {
      pii nex = q.p;
      int dir = q.bef ? 1 : -1;
      nex.y += dy[i];
      nex.x += dx[i] * dir;
      int bef = !q.bef;
      if (nex.y < 0 || nex.x < 0 || nex.y >= h || nex.x >= w)
        continue;
      if (inp[nex.y][nex.x] == 'T') {
        ret = min(ret, val);
        continue;
      } else if (inp[nex.y][nex.x] == 'S') {
        add = 0;
      } else if (inp[nex.y][nex.x] == 'X') {
        continue;
      } else {
        add = inp[nex.y][nex.x] - '0';
      }
      if (cost[nex.y][nex.x][bef] > val + add) {
        que.push(st{nex, bef, val + add});
        cost[nex.y][nex.x][bef] = val + add;
      }
    }
  }
  if (ret == INF)
    return -1;
  return ret;
}

int main() {
  while (cin >> w >> h, w || h) {
    inp.clear();
    inp.resize(h, vector<char>(w));
    rep(i, h) {
      rep(j, w) { cin >> inp[i][j]; }
    }
    vector<pii> vp;
    rep(i, h) rep(j, w) if (inp[i][j] == 'S') vp.push_back(pii(i, j));
    cout << solve(vp) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
#define INF 999999999
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (n); i++)
vector<vector<char>> inp;
int w, h;

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

struct st {
  pii p;
  int bef;
  int c;
  bool operator<(st a) const { return this->c > a.c; }
};
int cost[65][65][2];

#define y first
#define x second
int solve(vector<pii> poses) {
  fill(**cost, **cost + 65 * 65 * 2, INF);
  int ret = INF;
  priority_queue<st> que;
  for (int i = 0; i < poses.size(); i++) {
    pii pos = poses[i];
    que.push(st{pos, 0, 0});
    que.push(st{pos, 1, 0});
    cost[pos.y][pos.x][0] = 0;
    cost[pos.y][pos.x][1] = 0;
  }
  while (que.size()) {
    st q = que.top();
    que.pop();
    int val, add;
    val = q.c;
    rep(i, 9) {
      pii nex = q.p;
      int dir = q.bef ? 1 : -1;
      nex.y += dy[i];
      nex.x += dx[i] * dir;
      int bef = q.bef;
      if (nex.y < 0 || nex.x < 0 || nex.y >= h || nex.x >= w)
        continue;
      if (inp[nex.y][nex.x] == 'T') {
        ret = min(ret, val);
        continue;
      } else if (inp[nex.y][nex.x] == 'S') {
        add = 0;
      } else {
        add = inp[nex.y][nex.x] - '0';
      }
      if (inp[nex.y][nex.x] == 'X')
        continue;
      if (cost[nex.y][nex.x][bef] > val + add) {
        que.push(st{nex, !bef, val + add});
        cost[nex.y][nex.x][bef] = val + add;
      }
    }
  }
  if (ret == INF)
    return -1;
  return ret;
}

int main() {
  while (cin >> w >> h, w || h) {
    inp.resize(h, vector<char>(w));
    rep(i, h) {
      rep(j, w) { cin >> inp[i][j]; }
    }
    vector<pii> vp;
    rep(i, h) rep(j, w) if (inp[i][j] == 'S') vp.push_back(pii(i, j));
    cout << solve(vp) << endl;
  }
}