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

#include <bits/stdc++.h>
using namespace std;
#define MAX 505
#define INF (1 << 30)
typedef pair<int, int> P;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int W, H, N, sy, sx, ty, tx;
int t[MAX][MAX][2];
int d[MAX][MAX];

int change(char ch) {
  if (ch == 'U')
    return 0;
  if (ch == 'R')
    return 1;
  return (ch == 'D' ? 2 : 3);
}

int solve() {
  int h = MAX;
  d[sy][sx] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, sy * h + sx));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int y = p.second / h, x = p.second % h;
    int cost = p.first, ncost;
    if (y == ty && x == tx)
      return cost;
    if (cost > d[y][x])
      continue;
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || nx < 0)
        continue;
      if (ny > H || nx > W)
        continue;
      ncost = cost;
      if (i == 0)
        ncost += t[ny][x][1];
      else if (i == 1)
        ncost += t[y][x][0];
      else if (i == 2)
        ncost += t[y][x][1];
      else
        ncost += t[y][nx][0];
      if (ncost >= d[ny][nx])
        continue;
      d[ny][nx] = ncost;
      Q.push(P(ncost, ny * h + nx));
    }
  }
  return INF;
}

void calc() {
  string str;
  int y, x, T;
  cin >> x >> y >> T >> str;

  while (T--) {
    for (int i = 0; i < (int)str.size(); i++) {
      int dir = change(str[i]);
      int ny = y + dy[dir], nx = x + dx[dir];
      if (ny < 0 || nx < 0)
        continue;
      if (ny >= H || nx >= W)
        continue;
      if (dir == 0)
        t[y][x][0]++;
      else if (dir == 1)
        t[ny][nx][1]++;
      else if (dir == 2)
        t[ny][nx][0]++;
      else
        t[y][x][1]++;
      y = ny, x = nx;
    }
  }
}

void init() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
      t[i][j][0] = 0, t[i][j][1] = 0, d[i][j] = INF;
    }
  }
}

int main() {
  cin >> W >> H >> N;
  init();
  cin >> sx >> sy >> tx >> ty;
  for (int i = 0; i < N; i++)
    calc();
  cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 505
#define INF (1 << 24)
typedef pair<int, int> P;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int W, H, N, sy, sx, ty, tx;
int t[MAX][MAX][2];
int d[MAX][MAX];

int change(char ch) {
  if (ch == 'U')
    return 0;
  if (ch == 'R')
    return 1;
  return (ch == 'D' ? 2 : 3);
}

int solve() {
  int h = H + 1;
  d[sy][sx] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, sy * h + sx));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int y = p.second / h, x = p.second % h;
    int cost = p.first, ncost;
    if (y == ty && x == tx)
      return cost;
    if (cost > d[y][x])
      continue;
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || nx < 0)
        continue;
      if (ny > H || nx > W)
        continue;
      ncost = cost;
      if (i == 0)
        ncost += t[ny][x][1];
      else if (i == 1)
        ncost += t[y][x][0];
      else if (i == 2)
        ncost += t[y][x][1];
      else
        ncost += t[y][nx][0];
      if (ncost >= d[ny][nx])
        continue;
      d[ny][nx] = ncost;
      Q.push(P(ncost, ny * h + nx));
    }
  }
  return INF;
}

void calc() {
  string str;
  int y, x, T;
  cin >> x >> y >> T >> str;

  while (T--) {
    for (int i = 0; i < (int)str.size(); i++) {
      int dir = change(str[i]);
      int ny = y + dy[dir], nx = x + dx[dir];
      if (ny < 0 || nx < 0)
        continue;
      if (ny >= H || nx >= W)
        continue;
      if (dir == 0)
        t[y][x][0]++;
      else if (dir == 1)
        t[ny][nx][1]++;
      else if (dir == 2)
        t[ny][nx][0]++;
      else
        t[y][x][1]++;
      y = ny, x = nx;
    }
  }
}

void init() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
      t[i][j][0] = 0, t[i][j][1] = 0, d[i][j] = INF;
    }
  }
}

int main() {

  while (1) {
    cin >> W >> H >> N;
    if (W == 0 && H == 0 && N == 0)
      break;
    init();
    cin >> sx >> sy >> tx >> ty;
    for (int i = 0; i < N; i++)
      calc();
    cout << solve() << endl;
  }
  return 0;
}