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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

inline bool valid(int x, int y, int W, int H) {
  return x >= 0 && y >= 0 && x < W && y < H;
}
int ddx[4][2] = {
    {1, 1},
    {0, 1},
    {0, 0},
    {0, 1},
};
int ddy[4][2] = {{0, 1}, {1, 1}, {0, 1}, {0, 0}};
int ddr[4][2] = {{1, 3}, {0, 2}, {1, 3}, {0, 2}};

typedef pair<int, int> P;
typedef pair<int, P> S;

int main() {
  int W, H, N;
  while (cin >> W >> H >> N) {
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    int cost[555][555][4] = {};
    const int dx[4] = {1, 0, -1, 0};
    const int dy[4] = {0, 1, 0, -1};
    int ds[256] = {};
    ds['R'] = 0;
    ds['D'] = 1;
    ds['L'] = 2;
    ds['U'] = 3;

    REP(_, N) {
      int x, y, T;
      cin >> x >> y >> T;
      string s;
      cin >> s;
      REP(iter, T) {
        REP(i, s.size()) {
          int r = ds[s[i]];
          int nx = x + dx[r];
          int ny = y + dy[r];
          if (valid(nx, ny, W, H)) {
            REP(j, 2) { cost[y + ddy[r][j]][x + ddx[r][j]][ddr[r][j]]++; }
            x = nx, y = ny;
          }
        }
      }
    }

    priority_queue<S, vector<S>, greater<S>> que;
    int dist[555][555] = {};
    REP(y, H + 1) REP(x, W + 1) dist[y][x] = INF;
    dist[sy][sx] = 0;
    que.push(S(0, P(sx, sy)));
    while (!que.empty()) {
      S s = que.top();
      que.pop();
      int x = s.second.first;
      int y = s.second.second;
      int c = s.first;
      // printf("%d %d %d\\n", x, y, c);
      if (dist[y][x] < c)
        continue;
      if (x == gx && y == gy) {
        cout << c << endl;
        break;
      }
      REP(r, 4) {
        int nx = x + dx[r];
        int ny = y + dy[r];
        if (valid(nx, ny, W + 1, H + 1)) {
          int nc = c + cost[y][x][r];
          if (dist[ny][nx] > nc) {
            dist[ny][nx] = nc;
            que.push(S(nc, P(nx, ny)));
          }
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int W, H, N;

#define encode(y, x) ((y) << 10 | (x))
#define decode(v, y, x) (y = v >> 10, x = v & 1023)

typedef long long ll;
#define PACK(a, b) (ll(a) << 32 | (b))
#define FST(x) (x >> 32)
#define SND(x) (x & 0xFFFFFFFF)
int dxc[256] = {}, dyc[256] = {};

constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, -1, 0, 1};
constexpr int dx1[] = {0, 0, -1, -1};
constexpr int dy1[] = {0, -1, -1, 0};
constexpr int dx2[] = {0, -1, -1, 0};
constexpr int dy2[] = {-1, -1, 0, 0};

int main() {
  dyc[(int)'U'] = -1;
  dyc[(int)'D'] = +1;
  dxc[(int)'L'] = -1;
  dxc[(int)'R'] = +1;

  int sx, sy, tx, ty;
  scanf("%d%d%d%d%d%d%d", &W, &H, &N, &sx, &sy, &tx, &ty);
  int S = encode(sy, sx);
  int T = encode(ty, tx);

  map<ll, int> mm;

  rep(i, N) {
    int x, y, t;
    static char s[1024];
    scanf("%d%d%d%s", &x, &y, &t, s);
    rep(j, t) {
      for (int k = 0; s[k]; k++) {
        char c = s[k];
        int nx = x + dxc[(int)c], ny = y + dyc[(int)c];
        if (nx < 0 || ny < 0 || nx >= W || ny >= H)
          continue;
        int cur = encode(y, x);
        int nxt = encode(ny, nx);
        if (cur > nxt)
          swap(cur, nxt);
        mm[PACK(cur, nxt)]++;
        x = nx, y = ny;
      }
    }
  }

  int const inf = 1e8;
  int e = encode(H + 1, W + 1);
  static int dst1[1024 * 1024];
  rep(i, e) dst1[i] = inf;
  dst1[S] = 0;
  priority_queue<ll> q1;
  q1.emplace(PACK(0, S));
  while (q1.size()) {
    int cx, cy, cd;
    decode(SND(q1.top()), cy, cx);
    cd = -FST(q1.top());
    q1.pop();
    int cur = encode(cy, cx);
    if (cur == T)
      break;
    if (cd > dst1[cur])
      continue;
    {
      int nx = cx + dx[0];
      int ny = cy + dy[0];
      if (nx < 0 || ny < 0 || nx > W || ny > H)
        ;
      else {
        int nxt = encode(ny, nx);
        int ax = cx + dx1[0];
        int ay = cy + dy1[0];
        int bx = cx + dx2[0];
        int by = cy + dy2[0];
        int a = encode(ay, ax);
        int b = encode(by, bx);
        if (a > b)
          swap(a, b);
        int cost = mm[PACK(a, b)];
        if (dst1[nxt] > dst1[cur] + cost) {
          dst1[nxt] = dst1[cur] + cost;
          // int score = sqrt((ny-ty)*(ny-ty) + (nx-tx)*(nx-tx));
          q1.emplace(PACK(-dst1[nxt], nxt));
        }
      }
    }
    {
      int nx = cx + dx[1];
      int ny = cy + dy[1];
      if (nx < 0 || ny < 0 || nx > W || ny > H)
        ;
      else {
        int nxt = encode(ny, nx);
        int ax = cx + dx1[1];
        int ay = cy + dy1[1];
        int bx = cx + dx2[1];
        int by = cy + dy2[1];
        int a = encode(ay, ax);
        int b = encode(by, bx);
        if (a > b)
          swap(a, b);
        int cost = mm[PACK(a, b)];
        if (dst1[nxt] > dst1[cur] + cost) {
          dst1[nxt] = dst1[cur] + cost;
          // int score = sqrt((ny-ty)*(ny-ty) + (nx-tx)*(nx-tx));
          q1.emplace(PACK(-dst1[nxt], nxt));
        }
      }
    }
    {
      int nx = cx + dx[2];
      int ny = cy + dy[2];
      if (nx < 0 || ny < 0 || nx > W || ny > H)
        ;
      else {
        int nxt = encode(ny, nx);
        int ax = cx + dx1[2];
        int ay = cy + dy1[2];
        int bx = cx + dx2[2];
        int by = cy + dy2[2];
        int a = encode(ay, ax);
        int b = encode(by, bx);
        if (a > b)
          swap(a, b);
        int cost = mm[PACK(a, b)];
        if (dst1[nxt] > dst1[cur] + cost) {
          dst1[nxt] = dst1[cur] + cost;
          // int score = sqrt((ny-ty)*(ny-ty) + (nx-tx)*(nx-tx));
          q1.emplace(PACK(-dst1[nxt], nxt));
        }
      }
    }
    {
      int nx = cx + dx[3];
      int ny = cy + dy[3];
      if (nx < 0 || ny < 0 || nx > W || ny > H)
        ;
      else {
        int nxt = encode(ny, nx);
        int ax = cx + dx1[3];
        int ay = cy + dy1[3];
        int bx = cx + dx2[3];
        int by = cy + dy2[3];
        int a = encode(ay, ax);
        int b = encode(by, bx);
        if (a > b)
          swap(a, b);
        int cost = mm[PACK(a, b)];
        if (dst1[nxt] > dst1[cur] + cost) {
          dst1[nxt] = dst1[cur] + cost;
          // int score = sqrt((ny-ty)*(ny-ty) + (nx-tx)*(nx-tx));
          q1.emplace(PACK(-dst1[nxt], nxt));
        }
      }
    }
  }
  cout << dst1[T] << endl;
}