Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 500

int H, W;
vector<string> S;

int pos_to_id[MAX_N][MAX_N];
pair<int, int> id_to_pos[MAX_N];

int Dist[MAX_N][MAX_N];
int V;

void connect(int i, int j) {
  if (i > 0) {
    int next_i = i - 1;
    int next_j = j;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (i < H - 1) {
    int next_i = i + 1;
    int next_j = j;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (j > 0) {
    int next_i = i;
    int next_j = j - 1;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (j < W - 1) {
    int next_i = i;
    int next_j = j + 1;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  S.resize(H);
  REP(i, H) { cin >> S[i]; }

  MINUS(pos_to_id);
  int node_size = 0;
  int root = -1;
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '.') {
        pos_to_id[i][j] = node_size;
        id_to_pos[node_size] = make_pair(i, j);
        node_size++;
      }
    }
  }

  V = node_size;
  REP(i, V) {
    REP(j, V) { Dist[i][j] = (i == j) ? 0 : LLINF; }
  }
  REP(i, H) {
    REP(j, W) { connect(i, j); }
  }
  REP(k, V) {
    REP(i, V) {
      REP(j, V) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); }
    }
  }

  int ans = 0;
  REP(i, V) {
    REP(j, V) { ans = max(ans, Dist[i][j]); }
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 500

int H, W;
vector<string> S;

int pos_to_id[MAX_N][MAX_N];
pair<int, int> id_to_pos[MAX_N];

int Dist[MAX_N][MAX_N];
int V;

void connect(int i, int j) {
  if (i > 0) {
    int next_i = i - 1;
    int next_j = j;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (i < H - 1) {
    int next_i = i + 1;
    int next_j = j;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (j > 0) {
    int next_i = i;
    int next_j = j - 1;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
  if (j < W - 1) {
    int next_i = i;
    int next_j = j + 1;
    if (S[next_i][next_j] == '.') {
      Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1;
      Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1;
    }
  }
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  S.resize(H);
  REP(i, H) { cin >> S[i]; }

  MINUS(pos_to_id);
  int node_size = 0;
  int root = -1;
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '.') {
        pos_to_id[i][j] = node_size;
        id_to_pos[node_size] = make_pair(i, j);
        node_size++;
      }
    }
  }

  V = node_size;
  REP(i, V) {
    REP(j, V) { Dist[i][j] = (i == j) ? 0 : LLINF; }
  }
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '.') {
        connect(i, j);
      }
    }
  }
  REP(k, V) {
    REP(i, V) {
      REP(j, V) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); }
    }
  }

  int ans = 0;
  REP(i, V) {
    REP(j, V) { ans = max(ans, Dist[i][j]); }
  }

  printf("%lld\n", ans);

  return 0;
}