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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int w, h;

int table[35][35];
int c[4];
int dist[35][35][4];

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

struct state {
  int x, y, d, costs;
  bool operator<(const state &arg) const { return costs > arg.costs; }
};

void dijkstra() {
  rep(i, h) rep(j, w) rep(k, 4) dist[i][j][k] = inf;
  dist[0][0][1] = 0;
  state s;
  s.x = 0, s.y = 0, s.d = 1, s.costs = 0;
  priority_queue<state> q;
  q.push(s);
  while (!q.empty()) {
    state cur = q.top();
    q.pop();
    int x, y, d, costs;
    x = cur.x, y = cur.y, d = cur.d, costs = cur.costs;
    if (dist[y][x][d] < costs)
      continue;
    rep(i, 4) {
      int nx = x, ny = y, nd = d, ncosts = costs;
      nd = (nd + i) % 4;
      nx += dx[nd], ny += dy[nd];
      if (table[y][x] != i)
        ncosts += c[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (dist[ny][nx][nd] > ncosts) {
          dist[ny][nx][nd] = ncosts;
          state nexts;
          nexts.x = nx, nexts.y = ny, nexts.d = nd, nexts.costs = ncosts;
          q.push(nexts);
        }
      }
    }
  }
  return;
}

int main(void) {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0)
      break;
    rep(i, h) rep(j, w) scanf("%d", &table[i][j]);
    rep(i, 4) scanf("%d", &c[i]);
    dijkstra();
    int ans = inf;
    rep(i, 4) ans = min(ans, dist[h - 1][w - 1][i]);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int w, h;

int table[35][35];
int c[4];
int dist[35][35][4];

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

struct state {
  int x, y, d, costs;
  bool operator<(const state &arg) const { return costs < arg.costs; }
};

void dijkstra() {
  rep(i, h) rep(j, w) rep(k, 4) dist[i][j][k] = inf;
  dist[0][0][1] = 0;
  state s;
  s.x = 0, s.y = 0, s.d = 1, s.costs = 0;
  priority_queue<state> q;
  q.push(s);
  while (!q.empty()) {
    state cur = q.top();
    q.pop();
    int x, y, d, costs;
    x = cur.x, y = cur.y, d = cur.d, costs = cur.costs;
    if (dist[y][x][d] < costs)
      continue;
    rep(i, 4) {
      int nx = x, ny = y, nd = d, ncosts = costs;
      nd = (nd + i) % 4;
      nx += dx[nd], ny += dy[nd];
      if (table[y][x] != i)
        ncosts += c[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (dist[ny][nx][nd] > ncosts) {
          dist[ny][nx][nd] = ncosts;
          state nexts;
          nexts.x = nx, nexts.y = ny, nexts.d = nd, nexts.costs = ncosts;
          q.push(nexts);
        }
      }
    }
  }
  return;
}

int main(void) {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0)
      break;
    rep(i, h) rep(j, w) scanf("%d", &table[i][j]);
    rep(i, 4) scanf("%d", &c[i]);
    dijkstra();
    int ans = inf;
    rep(i, 4) ans = min(ans, dist[h - 1][w - 1][i]);
    printf("%d\\n", ans);
  }
  return 0;
}