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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 1000000000
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(all(c)), c.end())
#define pb push_back
using namespace std;
typedef pair<int, int> P;
typedef pair<bool, P> iP;
typedef pair<int, iP> iPi;
int w, h;
int dx[] = {1, 1, 1, 1, 1, 2, 2, 2, 3, -1, -1, -1, -1, -1, -2, -2, -2, -3},
    dy[] = {-2, -1, 0, 1, 2, -1, 0, 1, 0, -2, -1, 0, 1, 2, -1, 0, 1, 0};
int d[62][32][2];
map<P, bool> mp;
//(i,(b,((i,i),(i,i))))
bool inf(P a) {
  if (a.second >= 0 && a.second < w && a.first >= 0 && a.first < h)
    return true;
  return false;
}

int dij(vector<vector<int>> f, vector<P> v) {
  priority_queue<iPi, vector<iPi>, greater<iPi>> que;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      for (int z = 0; z < 2; z++)
        d[i][j][z] = INF;
  for (int i = 0; i < v.size(); i++) {
    que.push(iPi(0, iP(false, v[i])));
    que.push(iPi(0, iP(true, v[i])));
    d[v[i].first][v[i].second][0] = 0;
    d[v[i].first][v[i].second][1] = 0;
  }

  while (!que.empty()) {
    iPi p = que.top();
    P pos = p.second.second;
    bool which = p.second.first;
    int nc = p.first;
    que.pop();
    if (d[pos.first][pos.second][which] < nc) {
      continue;
    }
    if (mp[pos]) {
      return nc;
    }
    d[pos.first][pos.second][which] = nc;
    if (which)
      for (int i = 0; i < 9; i++) {
        int ny = pos.first + dy[i], nx = pos.second + dx[i];
        if (inf(P(ny, nx)) && d[ny][nx][which ^ 1] > nc + f[ny][nx]) {
          que.push(iPi(nc + f[ny][nx], iP(which ^ 1, P(ny, nx))));
        }
      }
    else
      for (int i = 9; i < 18; i++) {
        int ny = pos.first + dy[i], nx = pos.second + dx[i];
        if (inf(P(ny, nx)) && d[ny][nx][which ^ 1] > nc + f[ny][nx]) {
          que.push(iPi(nc + f[ny][nx], iP(which ^ 1, P(ny, nx))));
        }
      }
  }
  return -1;
}
int main() {
  while (cin >> w >> h, w || h) {
    vector<vector<int>> f(h, vector<int>(w));
    vector<P> v(2);
    mp.clear();
    v.clear();
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char c;
        cin >> c; // f[i][j];
        if (c == 'S') {
          v.pb(P(i, j));
          f[i][j] = 0;
        } else if (c == 'T') {
          mp[P(i, j)] = true;
          f[i][j] = 0;
        } else if (c == 'X') {
          f[i][j] = INF;
        } else {
          f[i][j] = c - '0';
        }
      }
    }
    sort(all(v));
    cout << dij(f, v) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define INF 100000000
#include <algorithm>
#include <map>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct pla {
  int lx, ly, rx, ry, nowc;
  pla() {}
  pla(int lx, int ly, int rx, int ry, int nowc)
      : lx(lx), ly(ly), rx(rx), ry(ry), nowc(nowc) {}
  bool operator<(const pla &x) const { return nowc > x.nowc; }
};
int main() {
  int w, h, dx[] = {1, 1, 1, 1, 1, 2, 2, 2, 3},
            dy[] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
  while (cin >> w >> h, w || h) {
    char s[70][40];
    rep(i, h) rep(j, w) { cin >> s[i][j]; }
    priority_queue<pla> que;
    int d[63][63][33][33];
    rep(i, h) rep(j, w) rep(k, h) rep(l, w) d[i][j][k][l] = INF;
    rep(i, w) {
      if (s[h - 1][i] == 'S') {
        for (int j = i + 1; j < w; j++) {
          if (s[h - 1][j] == 'S') {
            que.push(pla(i, h - 1, j, h - 1, 0));
            d[h - 1][i][h - 1][j] = 0;
          }
        }
      }
    }
    int llx, lly, rrx, rry, nrrc, nllc;
    rep(i, w) {
      if (s[h - 1][i] == 'S') {
        rep(j, 9) {
          rrx = i + dx[j];
          rry = h - 1 + dy[j];
          if (rrx < 0 || rrx > w - 1 || rry > h - 1 || rry < 0 ||
              s[rry][rrx] == 'X')
            continue;
          nrrc = isdigit(s[rry][rrx]) ? s[rry][rrx] - '0' : 0;
          que.push(pla(i, h - 1, rrx, rry, nrrc));
          d[h - 1][i][rry][rrx] = nrrc;
        }
        rep(j, 9) {
          llx = i - dx[j];
          lly = h - 1 + dy[j];
          if (llx < 0 || llx > w - 1 || lly > h - 1 || lly < 0 ||
              s[lly][llx] == 'X')
            continue;
          nllc = isdigit(s[lly][llx]) ? s[lly][llx] - '0' : 0;
          que.push(pla(llx, lly, i, h - 1, nllc));
          d[lly][llx][h - 1][i] = nllc;
        }
      }
    }
    bool flag = true;
    while (!que.empty()) {
      pla now = que.top();
      que.pop();
      // cout<<now.nowc<<' '<<now.lx<<' '<<now.ly<<endl;
      if (d[now.ly][now.lx][now.ry][now.rx] < now.nowc) {
        //	cout<<"77777"<<endl;
        continue;
      }
      if (s[now.ly][now.lx] == 'T' || s[now.ry][now.rx] == 'T') {
        flag = false;
        cout << now.nowc << endl;
        break;
      }
      rep(i, 9) {
        int nrx, nry, nrc;
        nrx = now.lx + dx[i];
        nry = now.ly + dy[i];
        if ((nrx == now.rx && nry == now.ry) || nry < 0 || nry > h - 1 ||
            nrx < 0 || nrx > w - 1 || s[nry][nrx] == 'X')
          continue;
        nrc = isdigit(s[nry][nrx]) ? s[nry][nrx] - '0' : 0;
        //	cout<<nrc<<endl;
        if (d[now.ly][now.lx][nry][nrx] > now.nowc + nrc) {
          que.push(pla(now.lx, now.ly, nrx, nry, now.nowc + nrc));
          d[now.ly][now.lx][nry][nrx] = now.nowc + nrc;
        }
      }
      rep(i, 9) {
        int nlx, nly, nlc;
        nlx = now.rx - dx[i];
        nly = now.ry + dy[i];
        if ((nlx == now.lx && nly == now.ly) || nly < 0 || nly > h - 1 ||
            nlx < 0 || nlx > w - 1 || s[nly][nlx] == 'X')
          continue;
        nlc = isdigit(s[nly][nlx]) ? s[nly][nlx] - '0' : 0;
        if (d[nly][nlx][now.ry][now.rx] > now.nowc + nlc) {
          que.push(pla(nlx, nly, now.rx, now.ry, now.nowc + nlc));
          d[nly][nlx][now.ry][now.rx] = now.nowc + nlc;
        }
      }
    }
    if (flag)
      cout << -1 << endl;
  }
  return 0;
}