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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int dx[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};
int dy[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
                {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int main() {
  int n, m;
  while (cin >> m >> n, n + m) {
    vector<vector<char>> in(n, vector<char>(m));
    rep(i, n) rep(j, m) cin >> in[i][j];
    int dp[62][32][2];
    rep(i, 62) rep(j, 32) rep(k, 2) dp[i][j][k] = inf;
    queue<pair<pii, pii>> q;
    rep(i, m) if (in[n - 1][i] == 'S') {
      dp[n - 1][i][0] = dp[n - 1][i][1] = 0;
      q.push(pair<pii, pii>(pii(n - 1, i), pii(0, 0))); // x,y,r or l,cost
      q.push(pair<pii, pii>(pii(n - 1, i), pii(1, 0))); // x,y,r or l,cost
    }
    while (!q.empty()) {
      pii a = (q.front()).first;
      pii b = (q.front()).second;
      q.pop();
      if (dp[a.first][a.second][b.first] < b.second)
        continue;
      //			cout<<a.first<<" "<<a.second<<" "<<b.first<<"
      //"<<b.second<<endl;
      rep(i, 9) {
        int nx = a.first + dx[b.first][i];
        int ny = a.second + dy[b.first][i];
        int cost = dp[a.first][a.second][b.first];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m)
          continue;
        if (isdigit(in[nx][ny]))
          cost += in[nx][ny] - '0';
        if (in[nx][ny] == 'X')
          continue;
        if (dp[nx][ny][b.first ^ 1] > cost) {
          dp[nx][ny][b.first ^ 1] = cost;
          // if(in[nx][ny]!='T')
          q.push(pair<pii, pii>(pii(nx, ny), pii(b.first ^ 1, cost)));
        }
      }
    }
    int out = inf;
    rep(i, m) if (in[0][i] == 'T') out =
        min(out, min(dp[0][i][0], dp[0][i][1]));
    if (out == inf)
      out = -1;
    cout << out << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int dx[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};
int dy[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
                {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int main() {
  int n, m;
  while (cin >> m >> n, n + m) {
    vector<vector<char>> in(n, vector<char>(m));
    rep(i, n) rep(j, m) cin >> in[i][j];
    int dp[32][62][2];
    rep(i, 32) rep(j, 62) rep(k, 2) dp[i][j][k] = inf;
    queue<pair<pii, pii>> q;
    rep(i, m) if (in[n - 1][i] == 'S') {
      dp[n - 1][i][0] = dp[n - 1][i][1] = 0;
      q.push(pair<pii, pii>(pii(n - 1, i), pii(0, 0))); // x,y,r or l,cost
      q.push(pair<pii, pii>(pii(n - 1, i), pii(1, 0))); // x,y,r or l,cost
    }
    while (!q.empty()) {
      pii a = (q.front()).first;
      pii b = (q.front()).second;
      q.pop();
      if (dp[a.first][a.second][b.first] < b.second)
        continue;
      //			cout<<a.first<<" "<<a.second<<" "<<b.first<<"
      //"<<b.second<<endl;
      rep(i, 9) {
        int nx = a.first + dx[b.first][i];
        int ny = a.second + dy[b.first][i];
        int cost = dp[a.first][a.second][b.first];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m)
          continue;
        if (isdigit(in[nx][ny]))
          cost += in[nx][ny] - '0';
        if (in[nx][ny] == 'X')
          continue;
        if (dp[nx][ny][b.first ^ 1] > cost) {
          dp[nx][ny][b.first ^ 1] = cost;
          // if(in[nx][ny]!='T')
          q.push(pair<pii, pii>(pii(nx, ny), pii(b.first ^ 1, cost)));
        }
      }
    }
    int out = inf;
    rep(i, m) if (in[0][i] == 'T') out =
        min(out, min(dp[0][i][0], dp[0][i][1]));
    if (out == inf)
      out = -1;
    cout << out << endl;
  }
}