Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\n";
  }
  return out;
}

using iPair = pair<int, int>;

vector<vector<pair<int, int>>> graph;
vector<int> dist;

void add_edge(int u, int v, int wt) {
  graph[u].push_back(make_pair(v, wt));
  graph[v].push_back(make_pair(u, wt));
}

int longest_path_in_min_moves(int V, int src) {
  priority_queue<iPair, vector<iPair>, greater<iPair>> pq;
  pq.push(make_pair(0, src));

  dist = vector<int>(V, INT_MAX);
  dist[src] = 0;

  while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();

    for (auto x : graph[u]) {
      int v = x.first;
      int weight = x.second;

      if (dist[v] > dist[u] + weight) {
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }

  int longest = 0;
  for (int i = 0; i < V; ++i) {
    if (dist[i] != INT_MAX) {
      longest = max(longest, dist[i]);
    }
  }

  return longest;
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> maze(h);
  for (int i = 0; i < h; ++i) {
    cin >> maze[i];
  }

  graph = vector<vector<pair<int, int>>>(h * w, vector<pair<int, int>>());
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (maze[i][j] == '.') {
        if (i > 0 && maze[i - 1][j] == '.') {
          add_edge(i * w + j, (i - 1) * w + j, 1);
        }
        if (i < h - 1 && maze[i + 1][j] == '.') {
          add_edge(i * w + j, (i + 1) * w + j, 1);
        }
        if (j > 0 && maze[i][j - 1] == '.') {
          add_edge(i * w + j, i * w + j - 1, 1);
        }
        if (j < w - 1 && maze[i][j + 1] == '.') {
          add_edge(i * w + j, i * w + j + 1, 1);
        }
      }
    }
  }

  int max_moves = -1;
  for (int i = 0; i < h * w; ++i) {
    for (int j = 0; j < h; ++j) {
      for (int k = 0; k < w; ++k) {
        max_moves = max(max_moves, longest_path_in_min_moves(h * w, i));
      }
    }
  }

  cout << max_moves << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #define cerr if(false)cerr
#define watch(x) cerr << "> " << #x << ": " << x << "\n";

using namespace std;

template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
  for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
    out << v[i] << " ";
  out << "\n";
  return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << "(" << v[i].first << ", " << v[i].second << ") ";
  out << "\n";
  return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    for (size_t j = 0; j < v[i].size(); ++j) {
      out << v[i][j] << " ";
    }
    out << "\n";
  }
  return out;
}

using iPair = pair<int, int>;

vector<vector<pair<int, int>>> graph;
vector<int> dist;

void add_edge(int u, int v, int wt) {
  graph[u].push_back(make_pair(v, wt));
  graph[v].push_back(make_pair(u, wt));
}

int longest_path_in_min_moves(int V, int src) {
  priority_queue<iPair, vector<iPair>, greater<iPair>> pq;
  pq.push(make_pair(0, src));

  dist = vector<int>(V, INT_MAX);
  dist[src] = 0;

  while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();

    for (auto x : graph[u]) {
      int v = x.first;
      int weight = x.second;

      if (dist[v] > dist[u] + weight) {
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }

  int longest = 0;
  for (int i = 0; i < V; ++i) {
    if (dist[i] != INT_MAX) {
      longest = max(longest, dist[i]);
    }
  }

  return longest;
}

int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> maze(h);
  for (int i = 0; i < h; ++i) {
    cin >> maze[i];
  }

  graph = vector<vector<pair<int, int>>>(h * w, vector<pair<int, int>>());
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (maze[i][j] == '.') {
        if (i > 0 && maze[i - 1][j] == '.') {
          add_edge(i * w + j, (i - 1) * w + j, 1);
        }
        if (i < h - 1 && maze[i + 1][j] == '.') {
          add_edge(i * w + j, (i + 1) * w + j, 1);
        }
        if (j > 0 && maze[i][j - 1] == '.') {
          add_edge(i * w + j, i * w + j - 1, 1);
        }
        if (j < w - 1 && maze[i][j + 1] == '.') {
          add_edge(i * w + j, i * w + j + 1, 1);
        }
      }
    }
  }

  int max_moves = -1;
  for (int i = 0; i < h * w; ++i) {
    max_moves = max(max_moves, longest_path_in_min_moves(h * w, i));
  }

  cout << max_moves << "\n";

  return 0;
}