Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
vector<vector<int>> g;
int ans = INF;
int h, w;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<vector<bool>> used;
signed main() {
  cin >> h >> w;
  vector<vector<int>> g(h, vector<int>(w, 1));
  vector<vector<bool>> used(h, vector<bool>(w, true));
  int sub = 0;
  rep(i, h) rep(j, w) {
    char f;
    cin >> f;
    if (f == '#') {
      g[i][j] = INF;
      sub++;
    }
  }
  int cnt = INF;
  int flag = 0;
  vvi d(h, vi(w, INF));
  d[0][0] = 0;
  priority_queue<piii, vector<piii>, greater<piii>> que;
  que.push(mp(0, mp(0, 0)));
  while (!que.empty()) {
    piii temp = que.top();
    que.pop();
    pii now = temp.second;
    int cost = temp.first;
    used[now.first][now.second] = false;
    if (now == mp(h - 1, w - 1))
      break;
    if (d[now.first][now.second] < cost)
      continue;
    rep(i, 4) {
      pii next = mp(now.first + dy[i], now.second + dx[i]);
      if ((0 <= next.first && next.first < h) &&
          (0 <= next.second && next.second < w)) {
        if (g[next.first][next.second] == INF)
          continue;
        if (used[next.first][next.second] == false)
          continue;
        if (d[next.first][next.second] < d[now.first][now.second] + 1)
          continue;
        d[next.first][next.second] = d[now.first][now.second] + 1;
        que.push(mp(d[next.first][next.second], next));
      }
    }
  }
  cnt = d[h - 1][w - 1];
  if (cnt == INF)
    cout << -1 << endl;
  else
    cout << h * w - cnt - 1 - sub << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
// #define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
vector<vector<int>> g;
int ans = INF;
int h, w;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<vector<bool>> used;
signed main() {
  cin >> h >> w;
  vector<vector<int>> g(h, vector<int>(w, 1));
  vector<vector<bool>> used(h, vector<bool>(w, true));
  int sub = 0;
  rep(i, h) rep(j, w) {
    char f;
    cin >> f;
    if (f == '#') {
      g[i][j] = INF;
      sub++;
    }
  }
  int cnt = INF;
  int flag = 0;
  vvi d(h, vi(w, INF));
  d[0][0] = 0;
  priority_queue<piii, vector<piii>, greater<piii>> que;
  que.push(mp(0, mp(0, 0)));
  while (!que.empty()) {
    piii temp = que.top();
    que.pop();
    pii now = temp.second;
    int cost = temp.first;
    used[now.first][now.second] = false;
    if (now == mp(h - 1, w - 1))
      break;
    if (d[now.first][now.second] < cost)
      continue;
    rep(i, 4) {
      pii next = mp(now.first + dy[i], now.second + dx[i]);
      if ((0 <= next.first && next.first < h) &&
          (0 <= next.second && next.second < w)) {
        if (g[next.first][next.second] == INF)
          continue;
        if (used[next.first][next.second] == false)
          continue;
        if (d[next.first][next.second] <= d[now.first][now.second] + 1)
          continue;
        d[next.first][next.second] = d[now.first][now.second] + 1;
        que.push(mp(d[next.first][next.second], next));
      }
    }
  }
  cnt = d[h - 1][w - 1];
  if (cnt == INF)
    cout << -1 << endl;
  else
    cout << h * w - cnt - 1 - sub << endl;
}