Problem p01732 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;
int done[510][510];

int cost[510][510][4];
int W, H, N;

int valid(int x, int y) {
  if (x < 0 || y < 0 || x >= W || y >= H)
    return 0;
  return 1;
}
int main() {
  cin >> W >> H >> N;
  int sx, sy;
  int gx, gy;
  cin >> sx >> sy;
  cin >> gx >> gy;

#define L 0
#define D 1
#define R 2
#define U 3

  for (int i = 0; i < N; i++) {
    int x, y;
    int t;
    string p;
    cin >> x >> y >> t >> p;
    for (int j = 0; j < t; j++) {
      for (int k = 0; k < p.size(); k++) {
        int tx = x;
        int ty = y;
        if (p[k] == 'L')
          tx--;
        if (p[k] == 'R')
          tx++;
        if (p[k] == 'U')
          ty--;
        if (p[k] == 'D')
          ty++;
        if (valid(tx, ty)) {
          if (p[k] == 'D') {
            cost[x][y + 1][R]++;
            cost[x + 1][y + 1][L]++;
          } else if (p[k] == 'R') {
            cost[x + 1][y][D]++;
            cost[x + 1][y + 1][U]++;

          } else if (p[k] == 'U') {
            cost[tx][ty + 1][R]++;
            cost[tx + 1][ty + 1][L]++;
          } else {
            cost[tx + 1][ty][D]++;
            cost[tx + 1][ty + 1][U]++;
          }
          x = tx;
          y = ty;
        }
      }
    }
  }
  for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++)
      done[i][j] = 1e9;
  /*
  ooo
  ooo
  ooo
  */
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> Q;
  Q.push({0, {sx, sy}});
  done[sx][sy] = 0;
  int ok = 0;
  while (Q.size()) {
    pair<int, P> q = Q.top();
    Q.pop();
    int x = q.second.first;
    int y = q.second.second;

    if (done[x][y] < q.first)
      continue;

    if (x == gx && y == gy) {
      cout << q.first << endl;
      return 0;
    }
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] < 0 || x + dx[i] > W || y + dy[i] < 0 || y + dy[i] > H)
        continue;
      int c = q.first + cost[x][y][i];
      if (done[x + dx[i]][y + dy[i]] > c) {
        done[x + dx[i]][y + dy[i]] = c;
        Q.push(make_pair(c, P(x + dx[i], y + dy[i])));
      }
    }
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;
typedef pair<P, P> P2;
map<pair<P, P>, int> cost;
int W, H, N;

int valid(int x, int y) {
  if (x < 0 || y < 0 || x >= W || y >= H)
    return 0;
  return 1;
}
int main() {
  cin >> W >> H >> N;
  int sx, sy;
  int gx, gy;
  cin >> sx >> sy;
  cin >> gx >> gy;
  /*
          0,0 -> 1,0
          1,0 -> 1,1
          0,0 -> 0,1

          0,0 -> 0,1
          0,1 -> 1,1

  */

  for (int i = 0; i < N; i++) {
    int x, y;
    int t;
    string p;
    cin >> x >> y >> t >> p;
    for (int j = 0; j < t; j++) {
      for (int k = 0; k < p.size(); k++) {
        int tx = x;
        int ty = y;
        if (p[k] == 'L')
          tx--;
        if (p[k] == 'R')
          tx++;
        if (p[k] == 'U')
          ty--;
        if (p[k] == 'D')
          ty++;
        if (valid(tx, ty)) {
          if (p[k] == 'D') {
            cost[P2(P(x + 1, y + 1), P(x, y + 1))]++;
            cost[P2(P(x, y + 1), P(x + 1, y + 1))]++;
          } else if (p[k] == 'R') {
            cost[P2(P(x + 1, y + 1), P(x + 1, y))]++;
            cost[P2(P(x + 1, y), P(x + 1, y + 1))]++;

          } else if (p[k] == 'U') {
            cost[P2(P(tx + 1, ty + 1), P(tx, ty + 1))]++;
            cost[P2(P(tx, ty + 1), P(tx + 1, ty + 1))]++;
          } else {
            cost[P2(P(tx + 1, ty + 1), P(tx + 1, ty))]++;
            cost[P2(P(tx + 1, ty), P(tx + 1, ty + 1))]++;
          }
          x = tx;
          y = ty;
        }
      }
    }
  }
  /*
  ooo
  ooo
  ooo
  */
  map<P, int> done;
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> Q;
  Q.push({0, {sx, sy}});
  int ok = 0;
  while (Q.size()) {
    pair<int, P> q = Q.top();
    Q.pop();
    int x = q.second.first;
    int y = q.second.second;
    if (x < 0 || x > W || y < 0 || y > H)
      continue;
    if (done[P(x, y)]++)
      continue;

    if (x == gx && y == gy) {
      cout << q.first << endl;
      return 0;
    }
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {

      int c = q.first + cost[P2(P(x, y), P(x + dx[i], y + dy[i]))];
      Q.push(make_pair(c, P(x + dx[i], y + dy[i])));
    }
  }
}