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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(x, y, w, h) (0 <= (x) && (x) < (w) && 0 <= (y) && (y) < (h))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
  int w, h, n, sx, sy, gx, gy;
  cin >> w >> h >> n >> sx >> sy >> gx >> gy;
  vvi cross_v(h, vi(w + 1, 0));
  vvi cross_w(h + 1, vi(w, 0));
  REP(i, n) {
    int ix, iy, time;
    cin >> ix >> iy >> time;
    string s;
    cin >> s;
    REP(j, time) {
      REP(k, s.length()) {
        int dir;
        switch (s[k]) {
        case 'L':
          dir = 0;
          break;
        case 'D':
          dir = 1;
          break;
        case 'R':
          dir = 2;
          break;
        case 'U':
          dir = 3;
          break;
        }
        if (WRAP(ix + dx[dir], iy + dy[dir], w, h)) {
          if (dir == 0)
            cross_v[iy][ix]++;
          if (dir == 1)
            cross_w[iy + 1][ix]++;
          if (dir == 2)
            cross_v[iy][ix + 1]++;
          if (dir == 3)
            cross_w[iy][ix]++;
          ix += dx[dir];
          iy += dy[dir];
        }
      }
    }
  }
  int ans = 0;
  vvi dp(h + 1, vi(w + 1 + 1, 1e9));
  dp[sy][sx] = 0;
  priority_queue<pair<int, pair<int, int>>> Q;
  Q.push({0, {sy, sx}});
  while (!Q.empty()) {
    pair<int, pair<int, int>> q = Q.top();
    Q.pop();
    q.first *= -1;
    int qy = q.second.first;
    int qx = q.second.second;
    if (qx == gx && qy == gy) {
      ans = q.first;
    }
    REP(i, 4) {
      int nx = qx + dx[i];
      int ny = qy + dy[i];
      if (WRAP(nx, ny, w + 1, h + 1)) {
        if (i == 0)
          if (dp[ny][nx] > dp[qy][qx] + cross_w[qy][qx - 1]) {
            dp[ny][nx] = dp[qy][qx] + cross_w[qy][qx - 1];
            Q.push({-dp[ny][nx], {ny, nx}});
          }
        if (i == 1)
          if (dp[ny][nx] > dp[qy][qx] + cross_v[qy][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_v[qy][qx];
            Q.push({-dp[ny][nx], {ny, nx}});
          }
        if (i == 2)
          if (dp[ny][nx] > dp[qy][qx] + cross_w[qy][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_w[qy][qx];
            Q.push({-dp[ny][nx], {ny, nx}});
          }
        if (i == 3)
          if (dp[ny][nx] > dp[qy][qx] + cross_v[qy - 1][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_v[qy - 1][qx];
            Q.push({-dp[ny][nx], {ny, nx}});
          }
      }
    }
  }
  cout << dp[gy][gx] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(x, y, w, h) (0 <= (x) && (x) < (w) && 0 <= (y) && (y) < (h))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
  int w, h, n, sx, sy, gx, gy;
  cin >> w >> h >> n >> sx >> sy >> gx >> gy;
  vvi cross_v(h, vi(w + 1, 0));
  vvi cross_w(h + 1, vi(w, 0));
  REP(i, n) {
    int ix, iy, time;
    cin >> ix >> iy >> time;
    string s;
    cin >> s;
    REP(j, time) {
      REP(k, s.length()) {
        int dir;
        switch (s[k]) {
        case 'L':
          dir = 0;
          break;
        case 'D':
          dir = 1;
          break;
        case 'R':
          dir = 2;
          break;
        case 'U':
          dir = 3;
          break;
        }
        if (WRAP(ix + dx[dir], iy + dy[dir], w, h)) {
          if (dir == 0)
            cross_v[iy][ix]++;
          if (dir == 1)
            cross_w[iy + 1][ix]++;
          if (dir == 2)
            cross_v[iy][ix + 1]++;
          if (dir == 3)
            cross_w[iy][ix]++;
          ix += dx[dir];
          iy += dy[dir];
        }
      }
    }
  }
  int ans = 0;
  vvi dp(h + 1, vi(w + 1 + 1, 1e9));
  dp[sy][sx] = 0;
  priority_queue<pair<int, pair<int, int>>> Q;
  Q.push({0, {sy, sx}});
  while (!Q.empty()) {
    pair<int, pair<int, int>> q = Q.top();
    Q.pop();
    int qy = q.second.first;
    int qx = q.second.second;
    if (qx == gx && qy == gy) {
      ans = q.first;
    }
    REP(i, 4) {
      int nx = qx + dx[i];
      int ny = qy + dy[i];
      if (WRAP(nx, ny, w + 1, h + 1)) {
        if (i == 0)
          if (dp[ny][nx] > dp[qy][qx] + cross_w[qy][qx - 1]) {
            dp[ny][nx] = dp[qy][qx] + cross_w[qy][qx - 1];
            Q.push({dp[ny][nx], {ny, nx}});
          }
        if (i == 1)
          if (dp[ny][nx] > dp[qy][qx] + cross_v[qy][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_v[qy][qx];
            Q.push({dp[ny][nx], {ny, nx}});
          }
        if (i == 2)
          if (dp[ny][nx] > dp[qy][qx] + cross_w[qy][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_w[qy][qx];
            Q.push({dp[ny][nx], {ny, nx}});
          }
        if (i == 3)
          if (dp[ny][nx] > dp[qy][qx] + cross_v[qy - 1][qx]) {
            dp[ny][nx] = dp[qy][qx] + cross_v[qy - 1][qx];
            Q.push({dp[ny][nx], {ny, nx}});
          }
      }
    }
  }
  cout << dp[gy][gx] << endl;
}