Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

bool toB(char c) {
  if (c == '#') {
    return true;
  } else {
    return false;
  }
}

int dir[4][2] = {
    {0, -1},
    {0, 1},
    {-1, 0},
    {1, 0},
};

int dmap[1005][1005];

struct node {
  int i, j;
  bool operator>(const node &other) const {
    if (i != other.i) {
      return i > other.i;
    } else if (this->j != other.j) {
      return j > other.j;
    } else {
      return false;
    }
  }

  bool operator<(const node &other) const {
    if (i != other.i) {
      return this->i < other.i;
    } else if (this->j != other.j) {
      return this->j < other.j;
    } else {
      return false;
    }
  }
};

using namespace std;
int main() {
  int H, W;
  string m[1005];
  scanf("%d %d", &H, &W);
  rep(i, H) { cin >> m[i]; }
  priority_queue<pair<int, node>, vector<pair<int, node>>,
                 greater<pair<int, node>>>
      pq;

  rep(i, H) {
    rep(j, W) {
      dmap[i][j] = 3000;
      if (toB(m[i][j])) {
        pq.push(make_pair(0, (node){i, j}));
      }
    }
  }

  int ans = 0;
  while (!pq.empty()) {
    pair<int, node> temp = pq.top();
    pq.pop();
    int dist = temp.first;
    node pl = temp.second;
    if (dmap[pl.i][pl.j] <= dist) {
      continue;
    }
    dmap[pl.i][pl.j] = dist;
    ans = max(ans, dist);
    rep(d, 4) {
      int ni = pl.i + dir[d][0];
      int nj = pl.j + dir[d][1];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W) {
        pq.push(make_pair(dist + 1, (node){ni, nj}));
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

bool toB(char c) {
  if (c == '#') {
    return true;
  } else {
    return false;
  }
}

int dir[4][2] = {
    {0, -1},
    {0, 1},
    {-1, 0},
    {1, 0},
};

int dmap[1005][1005];

struct node {
  int i, j;
  bool operator>(const node &other) const {
    if (i != other.i) {
      return i > other.i;
    } else if (this->j != other.j) {
      return j > other.j;
    } else {
      return false;
    }
  }

  bool operator<(const node &other) const {
    if (i != other.i) {
      return this->i < other.i;
    } else if (this->j != other.j) {
      return this->j < other.j;
    } else {
      return false;
    }
  }
};

using namespace std;
int main() {
  int H, W;
  string m[1005];
  scanf("%d %d", &H, &W);
  rep(i, H) { cin >> m[i]; }
  priority_queue<pair<int, node>, vector<pair<int, node>>,
                 greater<pair<int, node>>>
      pq;

  rep(i, H) {
    rep(j, W) {
      dmap[i][j] = 3000;
      if (toB(m[i][j])) {
        pq.push(make_pair(0, (node){i, j}));
      }
    }
  }

  int ans = 0;
  while (!pq.empty()) {
    pair<int, node> temp = pq.top();
    pq.pop();
    int dist = temp.first;
    node pl = temp.second;
    if (dmap[pl.i][pl.j] <= dist) {
      continue;
    }
    dmap[pl.i][pl.j] = dist;
    ans = max(ans, dist);
    rep(d, 4) {
      int ni = pl.i + dir[d][0];
      int nj = pl.j + dir[d][1];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W) {
        if (dmap[ni][nj] > dist + 1)
          pq.push(make_pair(dist + 1, (node){ni, nj}));
      }
    }
  }
  cout << ans << endl;
}