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

#include <iostream>
#include <queue>
#include <string.h>
#define inf 1000000001
#define f first
#define s second
#define mk make_pair
using namespace std;

int X[4] = {0, 1, 0, -1};
int Y[4] = {-1, 0, 1, 0};
int H, W;
int map[1002][1002] = {};
int memo[1002][1002] = {};

bool in(int y, int x) {
  if (y < 0 || x < 0 || y >= H || x >= W)
    return false;
  return true;
}

int md(char c) {
  if (c == 'U')
    return 0;
  if (c == 'R')
    return 1;
  if (c == 'D')
    return 2;
  if (c == 'L')
    return 3;
}

int main() {
  int N, si, sj, gi, gj, x, y, T;
  string s;

  cin >> W >> H >> N;
  H = H * 2 + 1;
  W = W * 2 + 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      map[i][j] = 0;
      memo[i][j] = inf;
    }
  }
  cin >> sj >> si >> gj >> gi;
  si *= 2;
  sj *= 2;
  gi *= 2;
  gj *= 2;

  for (int k = 0; k < N; k++) {
    cin >> x >> y;
    x = x * 2 + 1;
    y = y * 2 + 1;
    cin >> T >> s;
    for (int i = 0; i < T; i++) {
      for (int j = 0; j < s.length(); j++) {
        int muki = md(s[j]);
        int a = x + X[muki] * 2, b = y + Y[muki] * 2;
        if (in(b, a)) {
          map[y + Y[muki]][x + X[muki]]++;
          x = a;
          y = b;
        }
      }
    }
  }

  int ans = inf;
  priority_queue<pair<int, pair<int, int>>> Q;
  Q.push(mk(0, mk(si, sj)));
  memo[si][sj] = 0;
  while (!Q.empty()) {
    pair<int, pair<int, int>> u = Q.top();
    Q.pop();
    // cout<<u.y<<" "<<u.x<<" "<<u.cost<<" "<<memo[u.y][u.x]<<endl;
    if (u.s.f == gi && u.s.s == gj) {
      ans = min(ans, -u.f);
      break;
    }
    for (int k = 0; k < 4; k++) {
      int a = u.s.s + X[k] * 2, b = u.s.f + Y[k] * 2;
      if (in(b, a)) {
        if (-u.f + map[u.s.f + Y[k]][u.s.s + X[k]] < memo[b][a]) {
          memo[b][a] = -u.f + map[u.s.f + Y[k]][u.s.s + X[k]];
          Q.push(mk(-memo[b][a], mk(b, a)));
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string.h>
#define inf 1000000001
using namespace std;

int X[4] = {0, 1, 0, -1};
int Y[4] = {-1, 0, 1, 0};
int H, W;

class State {
public:
  int x, y, cost;
  State(int y, int x, int cost) : y(y), x(x), cost(cost) {}
};

bool in(int y, int x) {
  if (y < 0 || x < 0 || y >= H || x >= W)
    return false;
  return true;
}

int md(char c) {
  if (c == 'U')
    return 0;
  if (c == 'R')
    return 1;
  if (c == 'D')
    return 2;
  if (c == 'L')
    return 3;
}

int main() {
  int map[1002][1002] = {};
  int memo[1002][1002] = {};
  int N, si, sj, gi, gj, x, y, T;
  string s;

  cin >> W >> H >> N;
  H = H * 2 + 1;
  W = W * 2 + 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      map[i][j] = 0;
      memo[i][j] = inf;
    }
  }
  cin >> sj >> si >> gj >> gi;
  si *= 2;
  sj *= 2;
  gi *= 2;
  gj *= 2;

  for (int k = 0; k < N; k++) {
    cin >> x >> y;
    x = x * 2 + 1;
    y = y * 2 + 1;
    cin >> T >> s;
    for (int i = 0; i < T; i++) {
      for (int j = 0; j < s.length(); j++) {
        int muki = md(s[j]);
        int a = x + X[muki] * 2, b = y + Y[muki] * 2;
        if (in(b, a)) {
          map[y + Y[muki]][x + X[muki]]++;
          x = a;
          y = b;
        }
      }
    }
  }

  int ans = inf;
  queue<State> Q;
  Q.push(State(si, sj, 0));
  memo[si][sj] = 0;
  while (!Q.empty()) {
    State u = Q.front();
    Q.pop();
    // cout<<u.y<<" "<<u.x<<" "<<u.cost<<" "<<memo[u.y][u.x]<<endl;
    if (u.y == gi && u.x == gj) {
      ans = min(ans, u.cost);
      continue;
    }
    for (int k = 0; k < 4; k++) {
      int a = u.x + X[k] * 2, b = u.y + Y[k] * 2;
      if (in(b, a)) {
        if (u.cost + map[u.y + Y[k]][u.x + X[k]] < memo[b][a]) {
          memo[b][a] = u.cost + map[u.y + Y[k]][u.x + X[k]];
          Q.push(State(b, a, memo[b][a]));
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}