Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \
  do {                                                                         \
    cout << "[";                                                               \
    for (int count = (a); count < (b); ++count)                                \
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\n';

struct UnionFind {
private:
  vector<int> par, depth;

public:
  UnionFind(int n) {
    par.resize(n);
    depth = vector<int>(n, 0);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (depth[x] < depth[y])
      par[x] = y;
    else {
      par[y] = x;
      if (depth[x] == depth[y])
        depth[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

template <typename T>
void dijkstra(int s, vector<vector<pair<int, T>>> &edges, vector<T> &dist) {
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
  dist[s] = 0;
  que.push(make_pair(0, s));
  while (!que.empty()) {
    pair<ll, int> p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (pair<int, T> &e : edges[v]) {
      if (dist[e.first] > dist[v] + e.second) {
        dist[e.first] = dist[v] + e.second;
        que.push(make_pair(dist[e.first], e.first));
      }
    }
  }
}

int main() {
  int INF = 1e6;
  int h, w, d[2] = {0, 1};
  cin >> h >> w;
  int a = 0;
  string s[100];
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  if (s[0][0] == '#')
    a = 1;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[0][0] = 0;
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;
  que.push(make_pair(0, P(0, 0)));
  while (!que.empty()) {
    pair<int, P> f = que.top();
    P t = f.second;
    que.pop();
    for (int i = 0; i < 2; ++i) {
      int x = t.first + d[i], y = t.second + d[i ^ 1];
      if (x >= h || y >= w)
        continue;
      int b = f.first + (s[t.first][t.second] == '.' && s[x][y] == '#');
      if (dist[x][y] < b)
        continue;
      dist[x][y] = b;
      que.push(make_pair(b, P(x, y)));
    }
  }
  cout << dist[h - 1][w - 1] + a << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \
  do {                                                                         \
    cout << "[";                                                               \
    for (int count = (a); count < (b); ++count)                                \
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\n';

struct UnionFind {
private:
  vector<int> par, depth;

public:
  UnionFind(int n) {
    par.resize(n);
    depth = vector<int>(n, 0);
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (depth[x] < depth[y])
      par[x] = y;
    else {
      par[y] = x;
      if (depth[x] == depth[y])
        depth[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

template <typename T>
void dijkstra(int s, vector<vector<pair<int, T>>> &edges, vector<T> &dist) {
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
  dist[s] = 0;
  que.push(make_pair(0, s));
  while (!que.empty()) {
    pair<ll, int> p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (pair<int, T> &e : edges[v]) {
      if (dist[e.first] > dist[v] + e.second) {
        dist[e.first] = dist[v] + e.second;
        que.push(make_pair(dist[e.first], e.first));
      }
    }
  }
}

int main() {
  int INF = 1e6;
  int h, w, d[2] = {0, 1};
  cin >> h >> w;
  int a = 0;
  string s[100];
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  if (s[0][0] == '#')
    a = 1;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[0][0] = 0;
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;
  que.push(make_pair(0, P(0, 0)));
  while (!que.empty()) {
    pair<int, P> f = que.top();
    P t = f.second;
    que.pop();
    for (int i = 0; i < 2; ++i) {
      int x = t.first + d[i], y = t.second + d[i ^ 1];
      if (x >= h || y >= w)
        continue;
      int b = f.first + (s[t.first][t.second] == '.' && s[x][y] == '#');
      if (dist[x][y] > b) {
        dist[x][y] = b;
        que.push(make_pair(b, P(x, y)));
      }
    }
  }
  cout << dist[h - 1][w - 1] + a << endl;
}