Problem p00731 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1000000007;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
typedef long long ll;

struct State {
  int y, x, lr, c;
  State(int y, int x, int lr, int c) : y(y), x(x), lr(lr), c(c) {}
  bool operator<(const State &right) const { return c > right.c; }
};

int main() {
  while (1) {
    int W, H;
    cin >> W >> H;
    if (W == 0)
      break;

    char field[30][60];
    int minDist[30][60][2];
    memset(minDist, -1, sizeof(minDist));
    priority_queue<State> que;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        if (field[i][j] == 'S') {
          minDist[i][j][0] = 0;
          minDist[i][j][1] = 0;
          que.emplace(i, j, 0, 0);
          que.emplace(i, j, 1, 0);
        }
      }
    }

    int ans = -1;
    while (que.size()) {
      State s = que.top();
      que.pop();
      if (minDist[s.y][s.x][s.lr] < s.c)
        continue;
      if (field[s.y][s.x] == 'T') {
        ans = s.c;
        break;
      }
      int mul = s.lr == 0 ? 1 : -1;
      for (int dx = mul; abs(dx) <= 3; dx += mul) {
        for (int dy = -3 + abs(dx); abs(dy) + abs(dx) <= 3; dy++) {
          int ny = s.y + dy, nx = s.x + dx, nlr = (s.lr + 1) % 2, nc = s.c;
          ;
          if (ny < 0 || H <= ny || nx < 0 || W <= nx)
            continue;
          if (field[ny][nx] == 'X')
            continue;
          if ('1' <= field[ny][nx] && field[ny][nx] <= '9') {
            nc += field[ny][nx] - '0';
          }
          if (minDist[ny][nx][nlr] == -1 || minDist[ny][nx][nlr] > nc) {
            minDist[ny][nx][nlr] = nc;
            que.emplace(ny, nx, nlr, nc);
          }
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1000000007;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
typedef long long ll;

struct State {
  int y, x, lr, c;
  State(int y, int x, int lr, int c) : y(y), x(x), lr(lr), c(c) {}
  bool operator<(const State &right) const { return c > right.c; }
};

int main() {
  while (1) {
    int W, H;
    cin >> W >> H;
    if (W == 0)
      break;

    char field[60][30];
    int minDist[60][30][2];
    memset(minDist, -1, sizeof(minDist));
    priority_queue<State> que;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        if (field[i][j] == 'S') {
          minDist[i][j][0] = 0;
          minDist[i][j][1] = 0;
          que.emplace(i, j, 0, 0);
          que.emplace(i, j, 1, 0);
        }
      }
    }

    int ans = -1;
    while (que.size()) {
      State s = que.top();
      que.pop();
      if (minDist[s.y][s.x][s.lr] < s.c)
        continue;
      if (field[s.y][s.x] == 'T') {
        ans = s.c;
        break;
      }
      int mul = s.lr == 0 ? 1 : -1;
      for (int dx = mul; abs(dx) <= 3; dx += mul) {
        for (int dy = -3 + abs(dx); abs(dy) + abs(dx) <= 3; dy++) {
          int ny = s.y + dy, nx = s.x + dx, nlr = (s.lr + 1) % 2, nc = s.c;
          ;
          if (ny < 0 || H <= ny || nx < 0 || W <= nx)
            continue;
          if (field[ny][nx] == 'X')
            continue;
          if ('1' <= field[ny][nx] && field[ny][nx] <= '9') {
            nc += field[ny][nx] - '0';
          }
          if (minDist[ny][nx][nlr] == -1 || minDist[ny][nx][nlr] > nc) {
            minDist[ny][nx][nlr] = nc;
            que.emplace(ny, nx, nlr, nc);
          }
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}