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

#include <algorithm>
#include <iostream>
#include <queue>
#define INF (1e9)
#define H 60
#define W 30
using namespace std;
typedef pair<int, int> P;
typedef pair<char, P> P1;
typedef pair<int, P1> P2;
int dijkstra(int, int, char);
int w, h, dl[H][W], dr[H][W], ans;
char s[H][W];

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> s[i][j];
    ans = INF;
    for (int i = 0; i < w; i++)
      if (s[h - 1][i] == 'S')
        ans = min(ans, min(dijkstra(h - 1, i, 'l'), dijkstra(h - 1, i, 'r')));
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

int dijkstra(int sy, int sx, char lr) {
  priority_queue<P2> q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      dl[i][j] = dr[i][j] = INF;
  q.push(P2(0, P1(lr, P(sy, sx))));
  if (lr == 'l')
    dl[sy][sx] = 0;
  else
    dr[sy][sx] = 0;
  while (!q.empty()) {
    P2 t = q.top();
    q.pop();
    char c = t.second.first;
    int cost = -t.first, y = t.second.second.first, x = t.second.second.second;
    int dy[9] = {0, 0, 0, -1, -1, -2, 1, 1, 2},
        dx[9] = {1, 2, 3, 1, 2, 1, 1, 2, 1};
    if (c == 'l' && dl[y][x] < cost)
      continue;
    if (c == 'r' && dr[y][x] < cost)
      continue;
    for (int i = 0; i < 9; i++) {
      int ny, nx;
      ny = y + dy[i];
      if (c == 'l')
        nx = x + dx[i];
      else
        nx = x - dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || s[ny][nx] == 'X' ||
          s[ny][nx] == 'S')
        continue;
      if (s[ny][nx] == 'T') {
        if (c == 'l')
          dr[ny][nx] = min(dr[ny][nx], cost);
        else
          dl[ny][nx] = min(dl[ny][nx], cost);
      } else if (c == 'l' && dr[ny][nx] > cost + s[ny][nx] - '0') {
        dr[ny][nx] = cost + s[ny][nx] - '0';
        q.push(P2(-dr[ny][nx], P1('r', P(ny, nx))));
      } else if (c == 'r' && dl[ny][nx] > cost + s[ny][nx] - '0') {
        dl[ny][nx] = cost + s[ny][nx] - '0';
        q.push(P2(-dl[ny][nx], P1('l', P(ny, nx))));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < w; i++)
    if (s[0][i] == 'T')
      res = min(res, min(dl[0][i], dr[0][i]));
  return res;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#define INF (1e9)
#define H 60
#define W 30
using namespace std;
typedef pair<int, int> P;
typedef pair<char, P> P1;
typedef pair<int, P1> P2;
int dijkstra(int, int, char);
int w, h, dl[H][W], dr[H][W], ans;
char s[H][W];

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> s[i][j];
    ans = INF;
    for (int i = 0; i < w; i++)
      if (s[h - 1][i] == 'S')
        ans = min(ans, min(dijkstra(h - 1, i, 'l'), dijkstra(h - 1, i, 'r')));
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

int dijkstra(int sy, int sx, char lr) {
  priority_queue<P2> q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      dl[i][j] = dr[i][j] = INF;
  q.push(P2(0, P1(lr, P(sy, sx))));
  if (lr == 'l')
    dl[sy][sx] = 0;
  else
    dr[sy][sx] = 0;
  while (!q.empty()) {
    P2 t = q.top();
    q.pop();
    char c = t.second.first;
    int cost = t.first, y = t.second.second.first, x = t.second.second.second;
    int dy[9] = {0, 0, 0, -1, -1, -2, 1, 1, 2},
        dx[9] = {1, 2, 3, 1, 2, 1, 1, 2, 1};
    if (c == 'l' && dr[y][x] < cost)
      continue;
    if (c == 'r' && dl[y][x] < cost)
      continue;
    for (int i = 0; i < 9; i++) {
      int ny, nx;
      ny = y + dy[i];
      if (c == 'l')
        nx = x + dx[i];
      else
        nx = x - dx[i];
      if (ny < 0 || nx < 0 || h <= ny || w <= nx || s[ny][nx] == 'X' ||
          s[ny][nx] == 'S')
        continue;
      if (s[ny][nx] == 'T') {
        if (c == 'l')
          dr[ny][nx] = min(dr[ny][nx], cost);
        else
          dl[ny][nx] = min(dl[ny][nx], cost);
      } else if (c == 'l' && dr[ny][nx] > cost + s[ny][nx] - '0') {
        dr[ny][nx] = cost + s[ny][nx] - '0';
        q.push(P2(dr[ny][nx], P1('r', P(ny, nx))));
      } else if (c == 'r' && dl[ny][nx] > cost + s[ny][nx] - '0') {
        dl[ny][nx] = cost + s[ny][nx] - '0';
        q.push(P2(dl[ny][nx], P1('l', P(ny, nx))));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < w; i++)
    if (s[0][i] == 'T')
      res = min(res, min(dl[0][i], dr[0][i]));
  return res;
}