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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct State {
  int d, x, y, foot;
  State(int _d, int _x, int _y, int _foot) {
    d = _d;
    x = _x;
    y = _y;
    foot = _foot;
  }
};

int d[30][60][2];
vector<int> sv;

int dijkstra(vector<vector<int>> &cost, State start) {
  int res = 1000000;
  auto compare = [](const State &s1, const State &s2) { return s1.d > s2.d; };
  priority_queue<State, vector<State>, decltype(compare)> q(compare);
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 60; j++) {
      d[i][j][0] = d[i][j][1] = 1000000;
    }
  }
  d[start.x][start.y][start.foot == 1] = 0;
  q.push(start);
  int flag = 0;
  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (d[s.x][s.y][s.foot == 1] < s.d)
      continue;
    if (cost[s.x][s.y] == 0)
      return s.d;
    for (int mx = 1; mx <= 3; mx++) {
      for (int my = 3 - mx; my >= mx - 3; my--) {
        int next_x = s.x + mx * s.foot;
        int next_y = s.y + my;
        if (!(next_x >= 0 && next_x < cost.size() && next_y >= 0 &&
              next_y < cost[0].size()))
          continue;
        if (cost[next_x][next_y] == -1)
          continue;
        if (d[next_x][next_y][(-s.foot) == 1] >
            d[s.x][s.y][s.foot == 1] + cost[next_x][next_y]) {
          d[next_x][next_y][(-s.foot) == 1] =
              d[s.x][s.y][s.foot == 1] + cost[next_x][next_y];
          q.push(State(d[next_x][next_y][(-s.foot) == 1], next_x, next_y,
                       -s.foot));
        }
      }
    }
  }
  return res;
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> cliff(w, vector<int>(h));
    bool s_flag = 0, t_flag = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char c;
        cin >> c;
        if (c <= '9' && c >= '0') {
          cliff[j][i] = atoi(&c);
        } else if (c == 'S') {
          cliff[j][i] = -1;
          sv.push_back(j);
        } else if (c == 'X')
          cliff[j][i] = -1;
        else if (c == 'T')
          cliff[j][i] = 0;
      }
    }
    int ans = 1000000;
    for (auto start : sv) {
      int ans1 = min(dijkstra(cliff, State(0, start, h - 1, 1)),
                     dijkstra(cliff, State(0, start, h - 1, -1)));
      ans = min(ans, ans1);
    }
    if (ans == 1000000)
      ans = -1;
    cout << ans << endl;
    sv.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct State {
  int d, x, y, foot;
  State(int _d, int _x, int _y, int _foot) {
    d = _d;
    x = _x;
    y = _y;
    foot = _foot;
  }
};

int d[30][60][2];
vector<int> sv;

int dijkstra(vector<vector<int>> &cost, State start) {
  int res = 1000000;
  auto compare = [](const State &s1, const State &s2) { return s1.d < s2.d; };
  priority_queue<State, vector<State>, decltype(compare)> q(compare);
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 60; j++) {
      d[i][j][0] = d[i][j][1] = 1000000;
    }
  }
  d[start.x][start.y][start.foot == 1] = 0;
  q.push(start);
  int flag = 0;
  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (d[s.x][s.y][s.foot == 1] < s.d)
      continue;
    if (cost[s.x][s.y] == 0 && res > s.d)
      res = s.d;
    for (int mx = 1; mx <= 3; mx++) {
      for (int my = 3 - mx; my >= mx - 3; my--) {
        int next_x = s.x + mx * s.foot;
        int next_y = s.y + my;
        if (!(next_x >= 0 && next_x < cost.size() && next_y >= 0 &&
              next_y < cost[0].size()))
          continue;
        if (cost[next_x][next_y] == -1)
          continue;
        if (d[next_x][next_y][(-s.foot) == 1] >
            d[s.x][s.y][s.foot == 1] + cost[next_x][next_y]) {
          d[next_x][next_y][(-s.foot) == 1] =
              d[s.x][s.y][s.foot == 1] + cost[next_x][next_y];
          q.push(State(d[next_x][next_y][(-s.foot) == 1], next_x, next_y,
                       -s.foot));
        }
      }
    }
  }
  return res;
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> cliff(w, vector<int>(h));
    bool s_flag = 0, t_flag = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char c;
        cin >> c;
        if (c <= '9' && c >= '0') {
          cliff[j][i] = atoi(&c);
        } else if (c == 'S') {
          cliff[j][i] = -1;
          sv.push_back(j);
        } else if (c == 'X')
          cliff[j][i] = -1;
        else if (c == 'T')
          cliff[j][i] = 0;
      }
    }
    int ans = 1000000;
    for (auto start : sv) {
      int ans1 = min(dijkstra(cliff, State(0, start, h - 1, 1)),
                     dijkstra(cliff, State(0, start, h - 1, -1)));
      ans = min(ans, ans1);
    }
    if (ans == 1000000)
      ans = -1;
    cout << ans << endl;
    sv.clear();
  }
}