Problem p00737 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define INF 1e8
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
struct state {
  int cost;
  int x, y;
  int dir;
  state(int _x, int _y, int _dir, int _cost)
      : x(_x), y(_y), dir(_dir), cost(_cost) {}
  bool operator<(const state &r) const { return cost < r.cost; }
};

int main(void) {
  int w, h;
  while (cin >> w >> h, w) {
    int mem[35][35][4];
    int grid[35][35];
    int c[4];
    for (int i = 0; i <= h; i++)
      for (int j = 0; j <= w; j++)
        for (int k = 0; k < 4; k++)
          mem[i][j][k] = INF;
    for (int i = 0; i <= h + 1; i++)
      for (int j = 0; j <= w + 1; j++)
        grid[i][j] = INF;

    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> grid[i][j];
    for (int i = 0; i < 4; i++)
      cin >> c[i];
    int mincost = INF;
    priority_queue<state> pq;
    pq.push(state(1, 1, 1, 0));
    while (!pq.empty()) {
      state P = pq.top();
      pq.pop();
      //			cout << P.x << ":" << P.y << "->" << P.cost <<
      //endl;
      if (P.y == h && P.x == w) {
        mincost = min(mincost, P.cost);
        continue;
      }
      if (grid[P.y][P.x] == INF)
        continue;
      if (grid[P.y][P.x] != 4) {
        int nd = (P.dir + grid[P.y][P.x]) % 4;
        int nx = P.x + dx[nd];
        int ny = P.y + dy[nd];
        if (mem[ny][nx][nd] > P.cost) {
          mem[ny][nx][nd] = P.cost;
          pq.push(state(nx, ny, nd, P.cost));
        }
      }
      for (int i = 0; i < 4; i++) {
        int nd = (P.dir + i) % 4;
        int nx = P.x + dx[nd];
        int ny = P.y + dy[nd];

        if (mem[ny][nx][nd] > P.cost + c[i]) {
          mem[ny][nx][nd] = P.cost + c[i];
          pq.push(state(nx, ny, nd, P.cost + c[i]));
        }
      }
    }
    cout << mincost << endl;
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define INF 1e8
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
struct state {
  int cost;
  int x, y;
  int dir;
  state(int _x, int _y, int _dir, int _cost)
      : x(_x), y(_y), dir(_dir), cost(_cost) {}
  bool operator<(const state &r) const { return cost > r.cost; }
};

int main(void) {
  int w, h;
  while (cin >> w >> h, w) {
    int mem[35][35][4];
    int grid[35][35];
    int c[4];
    for (int i = 0; i <= h; i++)
      for (int j = 0; j <= w; j++)
        for (int k = 0; k < 4; k++)
          mem[i][j][k] = INF;
    for (int i = 0; i <= h + 1; i++)
      for (int j = 0; j <= w + 1; j++)
        grid[i][j] = INF;

    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> grid[i][j];
    for (int i = 0; i < 4; i++)
      cin >> c[i];
    int mincost = INF;
    priority_queue<state> pq;
    pq.push(state(1, 1, 1, 0));
    while (!pq.empty()) {
      state P = pq.top();
      pq.pop();
      //			cout << P.x << ":" << P.y << "->" << P.cost <<
      //endl;
      if (P.y == h && P.x == w) {
        mincost = min(mincost, P.cost);
        continue;
      }
      if (grid[P.y][P.x] == INF)
        continue;
      if (grid[P.y][P.x] != 4) {
        int nd = (P.dir + grid[P.y][P.x]) % 4;
        int nx = P.x + dx[nd];
        int ny = P.y + dy[nd];
        if (mem[ny][nx][nd] > P.cost) {
          mem[ny][nx][nd] = P.cost;
          pq.push(state(nx, ny, nd, P.cost));
        }
      }
      for (int i = 0; i < 4; i++) {
        int nd = (P.dir + i) % 4;
        int nx = P.x + dx[nd];
        int ny = P.y + dy[nd];

        if (mem[ny][nx][nd] > P.cost + c[i]) {
          mem[ny][nx][nd] = P.cost + c[i];
          pq.push(state(nx, ny, nd, P.cost + c[i]));
        }
      }
    }
    cout << mincost << endl;
  }
  return 0;
}