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

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

typedef pair<int, int> Pair;
struct Edge {
  int to, w;
  bool operator<(const Edge &e) const { return w > e.w; }
};

typedef vector<vector<Edge>> Graph;
const int INF = 1 << 28;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};

int dijkstra(int src, int dst, const Graph &g) {
  int n = g.size();
  vector<int> cost(n, INF);
  priority_queue<Edge> que;
  cost[src] = 0;
  que.push((Edge){src, 0});
  while (que.size()) {
    const Edge s = que.top();
    que.pop();
    if (cost[s.to] < s.w)
      continue;
    if (s.to == dst)
      return s.w;
    for (int i = 0; i < g[s.to].size(); ++i) {
      const Edge &e = g[s.to][i];
      const Edge t = {e.to, s.w + e.w};
      if (cost[t.to] <= t.w)
        continue;
      cost[t.to] = t.w;
      que.push(t);
    }
  }
  return -1;
}

int main() {
  for (int W, H, N; cin >> W >> H >> N;) {
    Pair s, t;
    cin >> s.first >> s.second >> t.first >> t.second;
    int cnt[501][501][4] = {};
    for (int i = 0; i < N; ++i) {
      int x, y;
      cin >> x >> y;
      int T;
      string S;
      cin >> T >> S;
      while (T--) {
        for (int j = 0; j < S.size(); ++j) {
          int k;
          if (S[j] == 'R')
            k = 0;
          if (S[j] == 'D')
            k = 1;
          if (S[j] == 'L')
            k = 2;
          if (S[j] == 'U')
            k = 3;
          int nx = x + dj[k];
          int ny = y + di[k];
          if (nx < 0 || nx >= W || ny < 0 || ny >= H) {
            continue;
          }
          if (ny == y) {
            int p = max(x, nx);
            cnt[y][p][1] += 1;
            cnt[y + 1][p][3] += 1;
          } else {
            int p = max(y, ny);
            cnt[p][x][0] += 1;
            cnt[p][x + 1][2] += 1;
          }
          x = nx;
          y = ny;
        }
      }
    }
    Graph G((H + 1) * (W + 1));
    for (int i = 0; i <= H; ++i) {
      for (int j = 0; j <= W; ++j) {
        for (int k = 0; k < 4; ++k) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (ni < 0 || ni >= H + 1)
            continue;
          if (nj < 0 || nj >= W + 1)
            continue;
          int a = i * (W + 1) + j;
          int b = ni * (W + 1) + nj;
          G[a].push_back((Edge){b, cnt[i][j][k]});
        }
      }
    }
    int src = s.second * (W + 1) + s.first;
    int dst = t.second * (W + 1) + t.first;
    cout << dijkstra(src, dst, G) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef pair<int, int> Pair;
struct Edge {
  int to, w;
  bool operator<(const Edge &e) const { return w > e.w; }
};

typedef vector<vector<Edge>> Graph;
const int INF = 1 << 28;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};

int dijkstra(int src, int dst, const Graph &g) {
  int n = g.size();
  vector<int> cost(n, INF);
  priority_queue<Edge> que;
  cost[src] = 0;
  que.push((Edge){src, 0});
  while (que.size()) {
    const Edge s = que.top();
    que.pop();
    if (cost[s.to] < s.w)
      continue;
    if (s.to == dst)
      return s.w;
    for (int i = 0; i < g[s.to].size(); ++i) {
      const Edge &e = g[s.to][i];
      const Edge t = {e.to, s.w + e.w};
      if (cost[t.to] <= t.w)
        continue;
      cost[t.to] = t.w;
      que.push(t);
    }
  }
  return -1;
}

int main() {
  for (int W, H, N; cin >> W >> H >> N;) {
    Pair s, t;
    cin >> s.first >> s.second >> t.first >> t.second;
    int cnt[500][500][4] = {};
    for (int i = 0; i < N; ++i) {
      int x, y;
      cin >> x >> y;
      int T;
      string S;
      cin >> T >> S;
      while (T--) {
        for (int j = 0; j < S.size(); ++j) {
          int k;
          if (S[j] == 'R')
            k = 0;
          if (S[j] == 'D')
            k = 1;
          if (S[j] == 'L')
            k = 2;
          if (S[j] == 'U')
            k = 3;
          int nx = x + dj[k];
          int ny = y + di[k];
          if (nx < 0 || nx >= W || ny < 0 || ny >= H) {
            continue;
          }
          if (ny == y) {
            int p = max(x, nx);
            cnt[y][p][1] += 1;
            cnt[y + 1][p][3] += 1;
          } else {
            int p = max(y, ny);
            cnt[p][x][0] += 1;
            cnt[p][x + 1][2] += 1;
          }
          x = nx;
          y = ny;
        }
      }
    }
    Graph G((H + 1) * (W + 1));
    for (int i = 0; i <= H; ++i) {
      for (int j = 0; j <= W; ++j) {
        for (int k = 0; k < 4; ++k) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (ni < 0 || ni >= H + 1)
            continue;
          if (nj < 0 || nj >= W + 1)
            continue;
          int a = i * (W + 1) + j;
          int b = ni * (W + 1) + nj;
          G[a].push_back((Edge){b, cnt[i][j][k]});
        }
      }
    }
    int src = s.second * (W + 1) + s.first;
    int dst = t.second * (W + 1) + t.first;
    cout << dijkstra(src, dst, G) << endl;
  }
  return 0;
}