Problem p00731 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};
#define all(u) begin(u), end(u)
#define mp make_pair
#define pb push_back
#define WMAX 30
#define HMAX 60

int W, H;
char S[HMAX][WMAX];
int B[HMAX][WMAX][2];

int main() {
  while (true) {
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    memset(B, -1, sizeof(B));
    queue<pi> Q;
    queue<bool> R;

    rep(i, H) {
      rep(j, W) {
        scanf(" %c", &S[i][j]);
        if (S[i][j] == 'S') {
          B[i][j][0] = 0;
          B[i][j][1] = 0;
          Q.push(pi(i, j));
          Q.push(pi(i, j));
          R.push(0);
          R.push(1);
        }
      }
    }

    int M = 114514;
    while (!Q.empty()) {
      int h = Q.front().first;
      int w = Q.front().second;
      Q.pop();
      bool r = R.front();
      R.pop();

      if (S[h][w] == 'T') {
        M = min(B[h][w][r], M);
        continue;
      }
      repi(i, h - 2, h + 3) {

        repi(j, w - 3, w + 4) {
          if (i < 0 || i >= H || j < 0 || j >= W)
            continue;
          if (r && j <= w)
            continue;
          if (!r && w <= j)
            continue;
          if (abs(h - i) + abs(w - j) > 3)
            continue;
          if (S[i][j] == 'X' || S[i][j] == 'S')
            continue;
          int dis;
          if (S[i][j] == 'T')
            dis = 0;
          else
            dis = S[i][j] - '0';
          if (B[i][j][!r] != -1 && B[i][j][!r] < B[h][w][r] + dis)
            continue;
          B[i][j][!r] = B[h][w][r] + dis;
          Q.push(pi(i, j));
          R.push(!r);
        }
      }
    }
    /* rep(i,H)
      {
        rep(j,W)
          {
            printf("%d ",min(B[i][j][0],B[i][j][1]));
          }
        printf("\n");
        }*/
    if (M == 114514)
      M = -1;
    printf("%d\n", M);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};
#define all(u) begin(u), end(u)
#define mp make_pair
#define pb push_back
#define WMAX 30
#define HMAX 60

int W, H;
char S[HMAX][WMAX];
int B[HMAX][WMAX][2];

int main() {
  while (true) {
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    memset(B, -1, sizeof(B));
    queue<pi> Q;
    queue<bool> R;

    rep(i, H) {
      rep(j, W) {
        scanf(" %c", &S[i][j]);
        if (S[i][j] == 'S') {
          B[i][j][0] = 0;
          B[i][j][1] = 0;
          Q.push(pi(i, j));
          Q.push(pi(i, j));
          R.push(0);
          R.push(1);
        }
      }
    }

    int M = 114514;
    while (!Q.empty()) {
      int h = Q.front().first;
      int w = Q.front().second;
      Q.pop();
      bool r = R.front();
      R.pop();

      if (S[h][w] == 'T') {
        M = min(B[h][w][r], M);
        continue;
      }
      repi(i, h - 2, h + 3) {

        repi(j, w - 3, w + 4) {
          if (i < 0 || i >= H || j < 0 || j >= W)
            continue;
          if (r && j <= w)
            continue;
          if (!r && w <= j)
            continue;
          if (abs(h - i) + abs(w - j) > 3)
            continue;
          if (S[i][j] == 'X' || S[i][j] == 'S')
            continue;
          int dis;
          if (S[i][j] == 'T')
            dis = 0;
          else
            dis = S[i][j] - '0';
          if (B[i][j][!r] != -1 && B[i][j][!r] <= B[h][w][r] + dis)
            continue;
          B[i][j][!r] = B[h][w][r] + dis;
          Q.push(pi(i, j));
          R.push(!r);
        }
      }
    }
    /* rep(i,H)
      {
        rep(j,W)
          {
            printf("%d ",min(B[i][j][0],B[i][j][1]));
          }
        printf("\n");
        }*/
    if (M == 114514)
      M = -1;
    printf("%d\n", M);
  }
  return 0;
}