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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h, w || h) {
    int d, n;
    cin >> d >> n;
    int a[20][20] = {};
    for (i = 0; i < n; ++i) {
      char s, t;
      int p, q;
      cin >> s >> t >> p >> q;
      p--;
      a[p][s - 'a'] = q;
    }
    int m;
    cin >> m;
    int b[20][21], c[21][20];
    memset(b, -1, sizeof(b));
    memset(c, -1, sizeof(c));
    for (i = 0; i < h; ++i)
      for (j = 1; j < w; ++j)
        b[i][j] = 0;
    for (i = 1; i < h; i++)
      for (j = 0; j < w; j++)
        c[i][j] = 0;
    for (i = 0; i < m; ++i) {
      char s, t, u;
      int p, q;
      cin >> s >> u >> p >> t >> u >> q;
      p--;
      q--;
      if (s == t) {
        c[max(p, q)][s - 'a'] = -1;
      } else {
        b[p][max(s, t) - 'a'] = -1;
      }
    }
    int o;
    cin >> o;
    for (i = 0; i < o; ++i) {
      char s, t, u;
      int p, q, r;
      cin >> s >> u >> p >> t >> u >> q >> r;
      p--;
      q--;
      if (s == t) {
        c[max(p, q)][s - 'a'] = r;
      } else {
        b[p][max(s, t) - 'a'] = r;
      }
    }
    char s, t;
    int sx, sy, tx, ty;
    cin >> s >> t >> sy;
    sx = s - 'a';
    sy--;
    cin >> s >> t >> ty;
    tx = s - 'a';
    ty--;
    int f[101][4][20][20];
    memset(f, -1, sizeof(f));
    priority_queue<pair<pair<int, int>, pair<int, int>>,
                   vector<pair<pair<int, int>, pair<int, int>>>,
                   greater<pair<pair<int, int>, pair<int, int>>>>
        e;
    e.push(make_pair(make_pair(0, 0), make_pair(sx, sy)));
    for (;;) {
      int x, y, z, t;
      t = e.top().first.first;
      z = e.top().first.second;
      x = e.top().second.first;
      y = e.top().second.second;
      if (x == tx && y == ty)
        break;
      e.pop();
      if (f[t][z][y][x] &&
          (a[y][x] == 0 ||
           z % 2 != (t /*+a[y][x]-1*/) % (a[y][x] * 2) / a[y][x])) {
        f[t][z][y][x] = 0;
        if (z != 3 && b[y][x + 1] >= 0 && t + d + b[y][x + 1] <= 100 &&
            f[t + d + b[y][x + 1]][1][y][x + 1])
          e.push(make_pair(make_pair(t + d + b[y][x + 1], 1),
                           make_pair(x + 1, y)));
        if (z != 1 && b[y][x] >= 0 && t + d + b[y][x] <= 100 &&
            f[t + d + b[y][x]][3][y][x - 1])
          e.push(make_pair(make_pair(t + d + b[y][x], 3), make_pair(x - 1, y)));
        if (z != 0 && c[y][x] >= 0 && t + d + c[y][x] <= 100 &&
            f[t + d + c[y][x]][2][y - 1][x])
          e.push(make_pair(make_pair(t + d + c[y][x], 2), make_pair(x, y - 1)));
        if (z != 2 && c[y + 1][x] >= 0 && t + d + c[y + 1][x] <= 100 &&
            f[t + d + c[y + 1][x]][0][y + 1][x])
          e.push(make_pair(make_pair(t + d + c[y + 1][x], 0),
                           make_pair(x, y + 1)));
      }
    }
    cout << e.top().first.first << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h, w || h) {
    int d, n;
    cin >> d >> n;
    int a[20][20] = {};
    for (i = 0; i < n; ++i) {
      char s, t;
      int p, q;
      cin >> s >> t >> p >> q;
      p--;
      a[p][s - 'a'] = q;
    }
    int m;
    cin >> m;
    int b[20][21], c[21][20];
    memset(b, -1, sizeof(b));
    memset(c, -1, sizeof(c));
    for (i = 0; i < h; ++i)
      for (j = 1; j < w; ++j)
        b[i][j] = 0;
    for (i = 1; i < h; i++)
      for (j = 0; j < w; j++)
        c[i][j] = 0;
    for (i = 0; i < m; ++i) {
      char s, t, u;
      int p, q;
      cin >> s >> u >> p >> t >> u >> q;
      p--;
      q--;
      if (s == t) {
        c[max(p, q)][s - 'a'] = -1;
      } else {
        b[p][max(s, t) - 'a'] = -1;
      }
    }
    int o;
    cin >> o;
    for (i = 0; i < o; ++i) {
      char s, t, u;
      int p, q, r;
      cin >> s >> u >> p >> t >> u >> q >> r;
      p--;
      q--;
      if (s == t) {
        c[max(p, q)][s - 'a'] = r;
      } else {
        b[p][max(s, t) - 'a'] = r;
      }
    }
    char s, t;
    int sx, sy, tx, ty;
    cin >> s >> t >> sy;
    sx = s - 'a';
    sy--;
    cin >> s >> t >> ty;
    tx = s - 'a';
    ty--;
    int f[101][4][20][20];
    memset(f, -1, sizeof(f));
    priority_queue<pair<pair<int, int>, pair<int, int>>,
                   vector<pair<pair<int, int>, pair<int, int>>>,
                   greater<pair<pair<int, int>, pair<int, int>>>>
        e;
    e.push(make_pair(make_pair(0, 2), make_pair(sx, sy)));
    for (;;) {
      int x, y, z, t;
      t = e.top().first.first;
      z = e.top().first.second;
      x = e.top().second.first;
      y = e.top().second.second;
      if (x == tx && y == ty)
        break;
      e.pop();
      if (f[t][z][y][x] &&
          (a[y][x] == 0 ||
           z % 2 == (t + a[y][x] - 1) % (a[y][x] * 2) / a[y][x])) {
        f[t][z][y][x] = 0;
        if (z != 2 && b[y][x + 1] >= 0 && t + d + b[y][x + 1] <= 100 &&
            f[t + d + b[y][x + 1]][0][y][x + 1])
          e.push(make_pair(make_pair(t + d + b[y][x + 1], 0),
                           make_pair(x + 1, y)));
        if (z != 0 && b[y][x] >= 0 && t + d + b[y][x] <= 100 &&
            f[t + d + b[y][x]][2][y][x - 1])
          e.push(make_pair(make_pair(t + d + b[y][x], 2), make_pair(x - 1, y)));
        if (z != 1 && c[y][x] >= 0 && t + d + c[y][x] <= 100 &&
            f[t + d + c[y][x]][3][y - 1][x])
          e.push(make_pair(make_pair(t + d + c[y][x], 3), make_pair(x, y - 1)));
        if (z != 3 && c[y + 1][x] >= 0 && t + d + c[y + 1][x] <= 100 &&
            f[t + d + c[y + 1][x]][1][y + 1][x])
          e.push(make_pair(make_pair(t + d + c[y + 1][x], 1),
                           make_pair(x, y + 1)));
      }
    }
    cout << e.top().first.first << endl;
  }
  return 0;
}