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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
//[int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[] = { 0, -1, 1, -1, 1, 0,
//-1, 1 };
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }

/*--------------------template--------------------*/
typedef int Weight;
struct Edge {
  int to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{to, cost});
}

void dijkstra(Graph &g, Array &d, int s) {
  d.assign(g.size(), INF);
  d[s] = 0;
  typedef pair<Weight, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  while (!que.empty()) {
    Weight dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist)
      continue;
    REP(i, g[v].size()) {
      Edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  int v = (w + 1) * (h + 1);
  Graph g(v);
  REP(i, h + 1) REP(j, w + 1) {
    REP(k, 4) {
      int nx = i + dx[k], ny = j + dy[k];
      if (!valid(nx, ny, h + 1, w + 1))
        continue;
      int from = place(i, j, w + 1), to = place(nx, ny, w + 1);
      add_edge(g, from, to, 0);
    }
  }
  REP(i, n) {
    int x, y, t;
    cin >> y >> x >> t;
    string s;
    cin >> s;
    REP(j, t) REP(k, s.size()) {
      if (s[k] == 'U') {
        int nx = x - 1, ny = y;
        if (!valid(nx, ny, h, w))
          continue;
        int l = x * (w + 1) + y, r = l + 1;
        REP(ii, g[l].size()) {
          if (g[l][ii].to == r) {
            g[l][ii].cost++;
            break;
          }
        }
        REP(ii, g[r].size()) {
          if (g[r][ii].to == l) {
            g[r][ii].cost++;
            break;
          }
        }
        x = nx, y = ny;
      } else if (s[k] == 'D') {
        int nx = x + 1, ny = y;
        if (!valid(nx, ny, h, w))
          continue;
        int l = nx * (w + 1) + ny, r = l + 1;
        REP(ii, g[l].size()) {
          if (g[l][ii].to == r) {
            g[l][ii].cost++;
            break;
          }
        }
        REP(ii, g[r].size()) {
          if (g[r][ii].to == l) {
            g[r][ii].cost++;
            break;
          }
        }
        x = nx, y = ny;
      } else if (s[k] == 'L') {
        int nx = x, ny = y - 1;
        if (!valid(nx, ny, h, w))
          continue;
        int l = x * (w + 1) + y, r = l + w + 1;
        REP(ii, g[l].size()) {
          if (g[l][ii].to == r) {
            g[l][ii].cost++;
            break;
          }
        }
        REP(ii, g[r].size()) {
          if (g[r][ii].to == l) {
            g[r][ii].cost++;
            break;
          }
        }
        x = nx, y = ny;
      } else {
        int nx = x, ny = y + 1;
        if (!valid(nx, ny, h, w))
          continue;
        int l = nx * (w + 1) + ny, r = l + w + 1;
        REP(ii, g[l].size()) {
          if (g[l][ii].to == r) {
            g[l][ii].cost++;
            break;
          }
        }
        REP(ii, g[r].size()) {
          if (g[r][ii].to == l) {
            g[r][ii].cost++;
            break;
          }
        }
        x = nx, y = ny;
      }
    }
  }

  Array dist;
  dijkstra(g, dist, place(sx, sy, w + 1));
  cout << dist[place(gx, gy, w + 1)] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
//[int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[] = { 0, -1, 1, -1, 1, 0,
//-1, 1 };
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }

/*--------------------template--------------------*/
typedef int Weight;
struct Edge {
  int to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{to, cost});
}

void dijkstra(Graph &g, Array &d, int s) {
  d.assign(g.size(), INF);
  d[s] = 0;
  typedef pair<Weight, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  while (!que.empty()) {
    Weight dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist)
      continue;
    REP(i, g[v].size()) {
      Edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  int v = (w + 1) * (h + 1);
  Graph g(v);
  map<pii, int> mp;
  REP(i, n) {
    int x, y, t;
    cin >> y >> x >> t;
    string s;
    cin >> s;
    REP(j, t) REP(k, s.size()) {
      if (s[k] == 'U') {
        int nx = x - 1, ny = y;
        if (!valid(nx, ny, h, w))
          continue;
        int l = x * (w + 1) + y, r = l + 1;
        mp[pii(l, r)]++;
        mp[pii(r, l)]++;
        x = nx, y = ny;
      } else if (s[k] == 'D') {
        int nx = x + 1, ny = y;
        if (!valid(nx, ny, h, w))
          continue;
        int l = nx * (w + 1) + ny, r = l + 1;
        mp[pii(l, r)]++;
        mp[pii(r, l)]++;
        x = nx, y = ny;
      } else if (s[k] == 'L') {
        int nx = x, ny = y - 1;
        if (!valid(nx, ny, h, w))
          continue;
        int l = x * (w + 1) + y, r = l + w + 1;
        mp[pii(l, r)]++;
        mp[pii(r, l)]++;
        x = nx, y = ny;
      } else {
        int nx = x, ny = y + 1;
        if (!valid(nx, ny, h, w))
          continue;
        int l = nx * (w + 1) + ny, r = l + w + 1;
        mp[pii(l, r)]++;
        mp[pii(r, l)]++;
        x = nx, y = ny;
      }
    }
  }
  REP(i, h + 1) REP(j, w + 1) {
    REP(k, 4) {
      int nx = i + dx[k], ny = j + dy[k];
      if (!valid(nx, ny, h + 1, w + 1))
        continue;
      int from = place(i, j, w + 1), to = place(nx, ny, w + 1);
      add_edge(g, from, to, mp[pii(from, to)]);
    }
  }
  Array dist;
  dijkstra(g, dist, place(sx, sy, w + 1));
  cout << dist[place(gx, gy, w + 1)] << endl;
  return 0;
}