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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int w, h;
char s[99][99];
int cost[60][30][60][30][2];

struct Node {
  int lx, ly, rx, ry;
  int foot, cost;
  bool operator<(const Node &r) const { return cost >= r.cost; }
};
bool ok(Node &node) {
  int lx = node.lx, rx = node.rx, ly = node.ly, ry = node.ry;
  if (lx < 0 || rx < 0 || ly < 0 || ry < 0 || lx >= w || rx >= w || ly >= h ||
      ry >= h)
    return 0;
  if (s[ly][lx] == 'X' || s[ry][rx] == 'X')
    return 0;
  return lx < rx && abs(lx - rx) + abs(ly - ry) <= 3;
}

int main() {
  while (cin >> w >> h, w) {

    rep(i, h) rep(j, w) rep(ii, h) rep(jj, w) rep(k, 2) cost[i][j][ii][jj][k] =
        inf;

    priority_queue<Node> Q;
    rep(i, h) rep(j, w) {
      cin >> s[i][j];
      if (s[i][j] == 'S') {
        Node node;
        node.ry = -inf, node.rx = -inf;
        node.ly = i, node.lx = j;
        node.foot = node.cost = 0;
        Q.push(node);
        swap(node.ry, node.ly);
        swap(node.rx, node.lx);
        node.foot = 1;
        Q.push(node);
      }
    }
    int ans = inf;
    while (!Q.empty()) {
      Node cur = Q.top();
      // dbg(cur.ly);dbg(cur.lx);dbg(cur.ry);dbg(cur.rx);
      // dbg(cur.cost);dbg(cur.foot);
      Q.pop();

      if (ok(cur) && (s[cur.ly][cur.lx] == 'T' || s[cur.ry][cur.rx] == 'T')) {
        ans = cur.cost;
        break;
      }

      for (int dy = -3; dy <= 3; dy++)
        for (int dx = -3; dx <= 3; dx++) {
          Node node = cur;
          int y, x;
          if (node.foot == 0)
            y = node.ry = node.ly + dy, x = node.rx = node.lx + dx;
          else
            y = node.ly = node.ry + dy, x = node.lx = node.rx + dx;
          node.foot ^= 1;
          if (!ok(node))
            continue;

          node.cost += isdigit(s[y][x]) ? s[y][x] - '0' : 0;
          int &c = cost[node.ly][node.lx][node.ry][node.rx][node.foot];
          if (node.cost >= c)
            continue;

          c = node.cost;
          Q.push(node);
        }
    }
    cout << (ans == inf ? -1 : ans) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int w, h;
char s[99][99];
int cost[60][30][60][30][2];

struct Node {
  int lx, ly, rx, ry;
  int foot, cost;
  bool operator<(const Node &r) const { return cost >= r.cost; }
};
bool ok(Node &node) {
  int lx = node.lx, rx = node.rx, ly = node.ly, ry = node.ry;
  if (lx < 0 || rx < 0 || ly < 0 || ry < 0 || lx >= w || rx >= w || ly >= h ||
      ry >= h)
    return 0;
  if (s[ly][lx] == 'X' || s[ry][rx] == 'X')
    return 0;
  return lx < rx && abs(lx - rx) + abs(ly - ry) <= 3;
}

int main() {
  while (cin >> w >> h, w) {

    rep(i, h) rep(j, w) rep(ii, h) rep(jj, w) rep(k, 2) cost[i][j][ii][jj][k] =
        inf;

    priority_queue<Node> Q;
    rep(i, h) rep(j, w) {
      cin >> s[i][j];
      if (s[i][j] == 'S') {
        Node node;
        node.ry = -inf, node.rx = -inf;
        node.ly = i, node.lx = j;
        node.foot = node.cost = 0;
        Q.push(node);
        swap(node.ry, node.ly);
        swap(node.rx, node.lx);
        node.foot = 1;
        Q.push(node);
      }
    }
    int ans = inf;
    while (!Q.empty()) {
      Node cur = Q.top();
      // dbg(cur.ly);dbg(cur.lx);dbg(cur.ry);dbg(cur.rx);
      // dbg(cur.cost);dbg(cur.foot);
      Q.pop();

      if (ok(cur) && (s[cur.ly][cur.lx] == 'T' || s[cur.ry][cur.rx] == 'T')) {
        ans = cur.cost;
        break;
      }

      rep(y, h) rep(x, w) {
        Node node = cur;
        if (node.foot == 0)
          node.ry = y, node.rx = x;
        else
          node.ly = y, node.lx = x;
        node.foot ^= 1;
        if (!ok(node))
          continue;

        node.cost += isdigit(s[y][x]) ? s[y][x] - '0' : 0;
        int &c = cost[node.ly][node.lx][node.ry][node.rx][node.foot];
        if (node.cost >= c)
          continue;

        c = node.cost;
        Q.push(node);
      }
    }
    cout << (ans == inf ? -1 : ans) << endl;
  }

  return 0;
}