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

#include <bits/stdc++.h>
using namespace std;

#define INF (1 << 29)

int W, H;
int C[5];
int S[50][50];
int D[50][50][4];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 2, 3};

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

bool outcheck(int x, int y) { return x < 0 || y < 0 || x >= W || y >= H; }

void solve() {

  priority_queue<state> q;
  for (int i = 0; i <= H; i++)
    for (int j = 0; j <= W; j++)
      for (int k = 0; k < 4; k++)
        D[j][i][k] = INF;
  q.push((state){0, 0, 1, 0});
  D[0][0][1] = 0;
  while (!q.empty()) {
    state p = q.top();
    q.pop();
    if (D[p.x][p.y][p.d] < p.c)
      continue;

    //    cout << p.x << "  " << p.y << " "<<p.d << endl;
    if (S[p.x][p.y] < 4) {
      int nd = (p.d + dc[S[p.x][p.y]]) % 4;
      int nx = p.x + dx[nd], ny = p.y + dy[nd];
      if (!outcheck(nx, ny)) {
        if (D[nx][ny][nd] > p.c) {
          D[nx][ny][nd] = p.c;
          q.push((state){nx, ny, nd, p.c});
        }
      }
    }

    for (int i = 0; i < 4; i++) {
      int nd = (p.d + dc[i]) % 4;
      int nx = p.x + dx[nd], ny = p.y + dy[nd];
      if (outcheck(nx, ny))
        continue;
      //  cout << D[nx][ny][nd] << " > " << p.c + C[i]<< " " << i  <<endl;
      if (D[nx][ny][nd] > p.c + C[i]) {
        // cout <<"next " << nx << " " << ny << " " << nd << endl;
        D[nx][ny][nd] = p.c + C[i];
        q.push((state){nx, ny, nd, p.c + C[i]});
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 4; i++) {
    res = min(res, D[W - 1][H - 1][i]);
  }
  cout << res << endl;
}

int main() {
  while (cin >> W >> H && (W || H)) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> S[j][i];
      }
    }
    for (int i = 0; i < 4; i++)
      cin >> C[i];

    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF (1 << 29)

int W, H;
int C[5];
int S[50][50];
int D[50][50][4];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 2, 3};

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

bool outcheck(int x, int y) { return x < 0 || y < 0 || x >= W || y >= H; }

void solve() {

  priority_queue<state, vector<state>, greater<state>> q;
  fill(D[0][0], D[50][50], INF);
  q.push((state){0, 0, 1, 0});
  D[0][0][1] = 0;
  while (!q.empty()) {
    state p = q.top();
    q.pop();
    if (D[p.x][p.y][p.d] < p.c)
      continue;

    //    cout << p.x << "  " << p.y << " "<<p.d << endl;
    if (S[p.x][p.y] < 4) {
      int nd = (p.d + dc[S[p.x][p.y]]) % 4;
      int nx = p.x + dx[nd], ny = p.y + dy[nd];
      if (!outcheck(nx, ny)) {
        if (D[nx][ny][nd] > p.c) {
          D[nx][ny][nd] = p.c;
          q.push((state){nx, ny, nd, p.c});
        }
      }
    }

    for (int i = 0; i < 4; i++) {
      int nd = (p.d + dc[i]) % 4;
      int nx = p.x + dx[nd], ny = p.y + dy[nd];
      if (outcheck(nx, ny))
        continue;
      //  cout << D[nx][ny][nd] << " > " << p.c + C[i]<< " " << i  <<endl;
      if (D[nx][ny][nd] > p.c + C[i]) {
        // cout <<"next " << nx << " " << ny << " " << nd << endl;
        D[nx][ny][nd] = p.c + C[i];
        q.push((state){nx, ny, nd, p.c + C[i]});
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 4; i++) {
    res = min(res, D[W - 1][H - 1][i]);
  }
  cout << res << endl;
}

int main() {
  while (cin >> W >> H && (W || H)) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> S[j][i];
      }
    }
    for (int i = 0; i < 4; i++)
      cin >> C[i];

    solve();
  }
}