Problem p00731 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

class state {
public:
  int foot, x, y, cost;
  state(int _f, int _x, int _y, int _c) {
    foot = _f;
    x = _x;
    y = _y;
    cost = _c;
  }
  bool operator<(const state &tmp) const { return cost > tmp.cost; }
};

int dx[2][9];
int dy[2][9];

void init() {
  int k = 0;
  for (int i = 1; i <= 3; i++) {
    for (int j = -2; j <= 2; j++)
      if (i + abs(j) <= 3) {
        dx[0][k] = i;
        dy[0][k] = j;
        dx[1][k] = -i;
        dy[1][k] = j;
        k++;
      }
  }
}

int atai(char c) {
  if ('0' <= c && c <= '9')
    return c - '0';
  return 0;
}
int main() {
  int w, h;
  char field[64][64];
  int memo[2][64][64];
  init();
  // 0 = right foot  , 1 = left foot

  while (scanf(" %d%d ", &w, &h), w | h) {
    rep(i, 64) rep(j, 64) memo[0][i][j] = memo[1][i][j] = INT_MAX;
    rep(i, h) rep(j, w) { scanf(" %c ", &field[i][j]); }
    priority_queue<state> q;
    rep(i, w) if (field[h - 1][i] == 'S') {
      q.push(state(0, i, h - 1, 0));
      q.push(state(1, i, h - 1, 0));
      memo[1][h - 1][i] = memo[0][h - 1][i] = 0;
    }
    int karikari = INT_MAX;
    while (!q.empty()) {
      int nx = q.top().x;
      int ny = q.top().y;
      int nf = q.top().foot;
      int nc = q.top().cost;
      if (karikari < nc)
        break;
      // printf("nc:%d\\n",nc);
      q.pop();
      rep(k, 9) {
        int tx = nx + dx[nf][k];
        int ty = ny + dy[nf][k];
        int next = (nf + 1) % 2;
        if (tx < 0 || ty < 0 || tx >= w || ty >= h || field[ty][tx] == 'X')
          continue;
        if (memo[next][ty][tx] > nc + atai(field[ty][tx])) {
          memo[next][ty][tx] = nc + atai(field[ty][tx]);
          q.push(state(next, tx, ty, nc + atai(field[ty][tx])));
          if (field[ty][tx] == 'T') {
            karikari = min(karikari, memo[next][ty][tx]);
            // printf("karikari:%d\\n",karikari);
          }
        }
      }
    }
    int ans = INT_MAX;
    rep(i, w) if (field[0][i] == 'T') {
      ans = min(ans, min(memo[0][0][i], memo[1][0][i]));
    }
    printf("%d\\n", ans == INT_MAX ? -1 : ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

class state {
public:
  int foot, x, y, cost;
  state(int _f, int _x, int _y, int _c) {
    foot = _f;
    x = _x;
    y = _y;
    cost = _c;
  }
  bool operator<(const state &tmp) const { return cost < tmp.cost; }
};

int dx[2][9];
int dy[2][9];

void init() {
  int k = 0;
  for (int i = 1; i <= 3; i++) {
    for (int j = -2; j <= 2; j++)
      if (i + abs(j) <= 3) {
        dx[0][k] = i;
        dy[0][k] = j;
        dx[1][k] = -i;
        dy[1][k] = j;
        k++;
      }
  }
}

int atai(char c) {
  if ('0' <= c && c <= '9')
    return c - '0';
  return 0;
}
int main() {
  int w, h;
  char field[64][64];
  int memo[2][64][64];
  init();
  // 0 = right foot  , 1 = left foot

  while (scanf(" %d%d ", &w, &h), w | h) {
    rep(i, 64) rep(j, 64) memo[0][i][j] = memo[1][i][j] = INT_MAX;
    rep(i, h) rep(j, w) { scanf(" %c ", &field[i][j]); }
    priority_queue<state> q;
    rep(i, w) if (field[h - 1][i] == 'S') {
      q.push(state(0, i, h - 1, 0));
      q.push(state(1, i, h - 1, 0));
      memo[1][h - 1][i] = memo[0][h - 1][i] = 0;
    }
    int karikari = INT_MAX;
    while (!q.empty()) {
      int nx = q.top().x;
      int ny = q.top().y;
      int nf = q.top().foot;
      int nc = q.top().cost;
      q.pop();
      rep(k, 9) {
        int tx = nx + dx[nf][k];
        int ty = ny + dy[nf][k];
        int next = (nf + 1) % 2;
        if (tx < 0 || ty < 0 || tx >= w || ty >= h || field[ty][tx] == 'X')
          continue;
        if (memo[next][ty][tx] > nc + atai(field[ty][tx])) {
          memo[next][ty][tx] = nc + atai(field[ty][tx]);
          q.push(state(next, tx, ty, nc + atai(field[ty][tx])));
          // if( field[ty][tx]=='T' ){
          //	karikari = min(karikari,nc);
          // }
        }
      }
    }
    int ans = INT_MAX;
    rep(i, w) if (field[0][i] == 'T') {
      ans = min(ans, min(memo[0][0][i], memo[1][0][i]));
    }
    printf("%d\\n", ans == INT_MAX ? -1 : ans);
  }
  return 0;
}