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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct aa {
  int x;
  int y;
  int time;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
  map<char, int> chmp;
  chmp['L'] = 0;
  chmp['D'] = 1;
  chmp['R'] = 2;
  chmp['U'] = 3;
  int W, H, N;
  cin >> W >> H >> N;
  vector<vector<vector<int>>> cells(H + 2,
                                    vector<vector<int>>(W + 2, vector<int>(4)));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  for (int i = 0; i < N; ++i) {
    int nowx, nowy;
    cin >> nowx >> nowy;
    nowx += 1;
    nowy += 1;
    int t;
    cin >> t;
    string st;
    cin >> st;
    for (int k = 0; k < t; ++k) {

      for (int j = 0; j < st.size(); ++j) {
        const int way = chmp[st[j]];
        int newx = nowx + dx[way];
        int newy = nowy + dy[way];
        if (newx < 1 || newx >= W + 1) {
          newx = nowx;
        } else if (newy < 1 || newy >= H + 1) {
          newy = nowy;
        } else {
          cells[nowy][nowx][way]++;
          cells[newy][newx][(2 + way) % 4]++;
        }
        nowx = newx;
        nowy = newy;
      }
    }
  }
  int memo[501][501];
  for (int x = 0; x < 501; ++x) {
    for (int y = 0; y < 501; ++y) {
      memo[y][x] = 1e9;
    }
  }
  memo[sy][sx] = 0;
  priority_queue<aa, vector<aa>, Compare> que;
  que.push(aa{sx, sy, 0});
  long long int ans = 0;
  while (!que.empty()) {
    aa atop(que.top());
    const int nowx = atop.x;
    const int nowy = atop.y;

    const int nowtime = atop.time;
    if (nowx == gx && nowy == gy) {
      ans = nowtime;
      break;
    }
    que.pop();
    for (int way = 0; way < 4; ++way) {
      const int newx = nowx + dx[way];
      const int newy = nowy + dy[way];
      if (newx < 0 || newx > W || newy < 0 || newy > H)
        continue;
      else {
        int nexttime = nowtime;
        if (way == 0) {
          nexttime += cells[nowy][nowx][1];
        } else if (way == 1) {
          nexttime += cells[nowy + 1][nowx][2];
        } else if (way == 2) {
          nexttime += cells[nowy + 1][nowx + 1][3];
        } else {
          nexttime += cells[nowy][nowx][2];
        }
        if (memo[newy][newx] > nexttime) {
          memo[newy][newx] = nexttime;
          que.push(aa{newx, newy, nexttime});
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct aa {
  int x;
  int y;
  int time;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
  map<char, int> chmp;
  chmp['L'] = 0;
  chmp['D'] = 1;
  chmp['R'] = 2;
  chmp['U'] = 3;
  int W, H, N;
  cin >> W >> H >> N;
  vector<vector<vector<int>>> cells(H + 2,
                                    vector<vector<int>>(W + 2, vector<int>(4)));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  for (int i = 0; i < N; ++i) {
    int nowx, nowy;
    cin >> nowx >> nowy;
    nowx += 1;
    nowy += 1;
    int t;
    cin >> t;
    string st;
    cin >> st;
    for (int k = 0; k < t; ++k) {

      for (int j = 0; j < st.size(); ++j) {
        const int way = chmp[st[j]];
        int newx = nowx + dx[way];
        int newy = nowy + dy[way];
        if (newx < 1 || newx >= W + 1) {
          newx = nowx;
        } else if (newy < 1 || newy >= H + 1) {
          newy = nowy;
        } else {
          cells[nowy][nowx][way]++;
          cells[newy][newx][(2 + way) % 4]++;
        }
        nowx = newx;
        nowy = newy;
      }
    }
  }
  int memo[501][501];
  for (int x = 0; x < 501; ++x) {
    for (int y = 0; y < 501; ++y) {
      memo[y][x] = 1e9;
    }
  }
  memo[sy][sx] = 0;
  priority_queue<aa, vector<aa>, Compare> que;
  que.push(aa{sx, sy, 0});
  long long int ans = 0;
  while (!que.empty()) {
    aa atop(que.top());
    const int nowx = atop.x;
    const int nowy = atop.y;

    const int nowtime = atop.time;
    if (nowx == gx && nowy == gy) {
      ans = nowtime;
      break;
    }
    que.pop();
    for (int way = 0; way < 4; ++way) {
      const int newx = nowx + dx[way];
      const int newy = nowy + dy[way];
      if (newx < 0 || newx > W || newy < 0 || newy > H)
        continue;
      else {
        int nexttime = nowtime;
        if (way == 0) {
          nexttime += cells[nowy - 1][nowx - 1][1];
        } else if (way == 1) {
          nexttime += cells[nowy][nowx - 1][2];
        } else if (way == 2) {
          nexttime += cells[nowy][nowx][3];
        } else {
          nexttime += cells[nowy - 1][nowx - 1][2];
        }
        if (memo[newy][newx] > nexttime) {
          memo[newy][newx] = nexttime;
          que.push(aa{newx, newy, nexttime});
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}