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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
#define MV 600
using namespace std;
typedef pair<int, int> P;
int W, H, N, Cs[2], Ce[2], X, Y, T,
    c1 = 0, c2 = 0, G[MV][MV] = {0}, V[MV][MV], v[MV][MV] = {0}, d[MV][MV],
    mx[4] = {0, 0, 1, -1}, my[4] = {1, -1, 0, 0}; // D,U,R,L
char S[1005], mp[255];
int main() {
  mp['D'] = 0;
  mp['U'] = 1;
  mp['R'] = 2;
  mp['L'] = 3;
  scanf("%d%d%d", &W, &H, &N);
  scanf("%d%d%d%d", Cs, Cs + 1, Ce, Ce + 1);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &X, &Y);
    scanf("%d%s", &T, S);
    for (int j = 0; j < T; j++) {
      for (int k = 0; S[k]; k++) {
        int nx = X + mx[mp[S[k]]];
        int ny = Y + my[mp[S[k]]];
        if (nx < 0 || nx >= W || ny < 0 || ny >= H)
          continue;
        if (nx == X)
          V[nx][max(ny, Y)]++;
        else
          G[max(nx, X)][ny]++;
        X = nx;
        Y = ny;
      }
    }
  }
  memset(d, 0x7f, sizeof(d));
  d[Cs[0]][Cs[1]] = 0;
  priority_queue<pair<int, P>> Q;
  Q.push(make_pair(0, make_pair(Cs[0], Cs[1])));
  for (; Q.size();) {
    int row = Q.top().second.first;
    int col = Q.top().second.second;
    int nxc = -Q.top().first;
    Q.pop();
    if (v[row][col])
      continue;
    v[row][col] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = row + mx[i];
      int ny = col + my[i];
      int nc = 0;
      if (nx == row)
        nc = G[nx][min(ny, col)];
      else
        nc = V[min(nx, row)][ny];
      if (nx < 0 || nx > W || ny < 0 || ny > H || v[nx][ny] ||
          d[nx][ny] <= nxc + nc)
        continue;
      d[nx][ny] = nxc + nc;
      Q.push(make_pair(-d[nx][ny], make_pair(nx, ny)));
    }
  }
  printf("%d\\n", d[Ce[0]][Ce[1]]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
#define MV 505
using namespace std;
typedef pair<int, int> P;
int W, H, N, Cs[2], Ce[2], i, j, k, X, Y, T,
    c1 = 0, c2 = 0, G[MV][MV] = {0}, V[MV][MV], v[MV][MV], d[MV][MV],
    mx[4] = {0, 0, 1, -1}, my[4] = {1, -1, 0, 0}; // D,U,R,L
char S[1005], mp[255];
int main() {
  mp['D'] = 0;
  mp['U'] = 1;
  mp['R'] = 2;
  mp['L'] = 3;
  scanf("%d%d%d", &W, &H, &N);
  scanf("%d%d%d%d", Cs, Cs + 1, Ce, Ce + 1);
  for (i = 0; i < N; i++) {
    scanf("%d%d", &X, &Y);
    scanf("%d%s", &T, S);
    for (j = 0; j < T; j++) {
      for (k = 0; k < strlen(S); k++) {
        int nx = X + mx[mp[S[k]]];
        int ny = Y + my[mp[S[k]]];
        if (nx < 0 || nx >= W || ny < 0 || ny >= H)
          continue;
        if (nx == X)
          V[nx][max(ny, Y)]++;
        else
          G[max(nx, X)][ny]++;
        X = nx;
        Y = ny;
      }
    }
  }
  for (int i = 0; i <= W; i++)
    for (int j = 0; j <= H; j++)
      d[i][j] = 999999999;
  d[Cs[0]][Cs[1]] = 0;
  priority_queue<pair<int, P>> Q;
  Q.push(make_pair(0, make_pair(Cs[0], Cs[1])));
  for (; Q.size();) {
    P np = Q.top().second;
    int nxc = -Q.top().first;
    Q.pop();
    if (v[np.first][np.second])
      continue;
    v[np.first][np.second] = 1;
    for (i = 0; i < 4; i++) {
      int ny = np.second + my[i];
      int nx = np.first + mx[i];
      int nc = 0;
      if (nx == np.first)
        nc = G[nx][min(ny, np.second)];
      else
        nc = V[min(nx, np.first)][ny];
      if (nx < 0 || nx > W || ny < 0 || ny > H || v[nx][ny] ||
          d[nx][ny] <= nxc + nc)
        continue;
      d[nx][ny] = nxc + nc;
      Q.push(make_pair(-d[nx][ny], make_pair(nx, ny)));
    }
  }
  printf("%d\\n", d[Ce[0]][Ce[1]]);
  return 0;
}