Problem p00737 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define M 30
#define INF 10000000

#define min(a, b) ((a) > (b) ? (b) : (a))

typedef pair<int, int> P;
typedef pair<P, P> PP;

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

bool inRange(int x, int y, int w, int h) {
  return (0 <= x && x < w && 0 <= y && y < h);
}

int main() {
  priority_queue<PP, vector<PP>, greater<PP>> q;
  int w, h, map[M][M], c[4], cost[M][M][4];
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> map[i][j];
        for (int k = 0; k < 4; ++k)
          cost[i][j][k] = INF;
      }
    }
    for (int i = 0; i < 4; ++i)
      cin >> c[i];

    // dijkstra
    q.push(PP(P(0, 0), P(0, 0)));
    cost[0][0][0] = 0;
    while (!q.empty()) {
      int co = q.top().first.first, dir = q.top().first.second;
      int x = q.top().second.first, y = q.top().second.second;
      q.pop();
      if (co > cost[y][x][dir])
        continue;
      for (int k = 0; k < 4; ++k) {
        int d = (dir + k) % 4;
        if (!inRange(x + dx[d], y + dy[d], w, h))
          continue;
        if (map[y][x] == k) {
          if (cost[y][x][dir] < cost[y + dy[d]][x + dx[d]][d]) {
            cost[y + dy[d]][x + dx[d]][d] = cost[y][x][dir];
            q.push(PP(P(cost[y + dy[d]][x + dx[d]][d], d),
                      P(x + dx[d], y + dy[d])));
          }
        } else {
          if (cost[y][x][dir] + c[k] < cost[y + dy[d]][x + dx[d]][d]) {
            cost[y + dy[d]][x + dx[d]][d] = cost[y][x][dir] + c[k];
            q.push(PP(P(cost[y + dy[d]][x + dx[d]][d], d),
                      P(x + dx[d], y + dy[d])));
          }
        }
      }
    }
    cout << min(cost[h - 1][w - 1][0], cost[h - 1][w - 1][1]) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

#define M 30
#define INF 10000000

#define min(a, b) ((a) > (b) ? (b) : (a))

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

int main() {
  int map[M + 4][M + 4], c[4], w, h;
  int dp[M + 4][M + 4][M * M * 2 + 10][5], ans;
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> map[i][j];
        for (int t = 0; t <= w * h; ++t) {
          for (int k = 0; k < 4; ++k)
            dp[i][j][t][k] = INF;
        }
      }
    }
    for (int i = 0; i < 4; ++i)
      cin >> c[i];
    dp[1][1][0][0] = 0;
    ans = INF;
    for (int t = 1; t <= w * h * 2; ++t) {
      for (int i = 1; i <= h; ++i) {
        for (int j = 1; j <= w; ++j) {
          if (i == h && j == w)
            continue;
          for (int k = 0; k < 4; ++k) {
            for (int l = 0; l < 4; ++l) {
              int d = (k + l) % 4;
              if (map[i][j] == l) {
                dp[i + dy[d]][j + dx[d]][t][d] =
                    min(dp[i + dy[d]][j + dx[d]][t][d], dp[i][j][t - 1][k]);
              } else {
                dp[i + dy[d]][j + dx[d]][t][d] = min(
                    dp[i + dy[d]][j + dx[d]][t][d], dp[i][j][t - 1][k] + c[l]);
              }
              if (j + dx[d] == w && i + dy[d] == h) {
                ans = min(ans, dp[i + dy[d]][j + dx[d]][t][d]);
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}