Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128

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

signed main() {
  freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w;
  cin >> h >> w;
  string s;
  memset(dp, 0x3f, sizeof(dp));
  priority_queue<pii, std::vector<pii>, greater<pii>> pq;
  for (int i = 0; i < h; ++i) {
    cin >> s;
    for (int j = 0; j < w; ++j) {
      if (s[j] == '#') {
        dp[i][j] = 0;
        pq.push(pii(0, i * w + j));
      }
    }
  }
  while (!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    int x = cur.se / w, y = cur.se % w;
    if (dp[x][y] < cur.fi)
      continue;
    // cout << x << ' ' << y << ' ' << cur.fi << endl;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (0 <= tx && tx < h && 0 <= ty && ty < w) {
        if (dp[tx][ty] > cur.fi + 1) {
          dp[tx][ty] = cur.fi + 1;
          pq.push(pii(cur.fi + 1, tx * w + ty));
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < h; ++i) {
    res = max(res, *max_element(dp[i], dp[i] + w));
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128

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

signed main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w;
  cin >> h >> w;
  string s;
  memset(dp, 0x3f, sizeof(dp));
  priority_queue<pii, std::vector<pii>, greater<pii>> pq;
  for (int i = 0; i < h; ++i) {
    cin >> s;
    for (int j = 0; j < w; ++j) {
      if (s[j] == '#') {
        dp[i][j] = 0;
        pq.push(pii(0, i * w + j));
      }
    }
  }
  while (!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    int x = cur.se / w, y = cur.se % w;
    if (dp[x][y] < cur.fi)
      continue;
    // cout << x << ' ' << y << ' ' << cur.fi << endl;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (0 <= tx && tx < h && 0 <= ty && ty < w) {
        if (dp[tx][ty] > cur.fi + 1) {
          dp[tx][ty] = cur.fi + 1;
          pq.push(pii(cur.fi + 1, tx * w + ty));
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < h; ++i) {
    res = max(res, *max_element(dp[i], dp[i] + w));
  }
  cout << res << endl;
  return 0;
}