Problem p01342 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

typedef vector<vector<int>> graph;

inline int get_index(int x, int y, int w) { return x + y * w; }

inline void add_edge(graph &G, int a, int b) {
  G[a].emplace_back(b);
  G[b].emplace_back(a);
}

inline int bfs(int s, int g, vector<int> &dist, const graph &G, int ng_x = -1,
               int ng_y = -1) {
  const int n = G.size();

  dist.assign(n, -1);
  queue<int> que;

  dist[s] = 0;
  que.push(s);

  while (!que.empty()) {
    const int v = que.front();
    que.pop();

    if (v == g)
      return dist[v];

    for (const auto &to : G[v]) {
      if (v == ng_x && to == ng_y)
        continue;

      if (dist[to] == -1) {
        dist[to] = dist[v] + 1;
        que.push(to);
      }
    }
  }

  return -1;
}

vector<int> get_path(int v, const vector<int> &dist, const graph &G) {
  vector<int> res{v};
  while (dist[v] > 0) {
    for (const auto &to : G[v]) {
      if (dist[to] + 1 == dist[v]) {
        v = to;
        res.emplace_back(to);
        break;
      }
    }
  }
  reverse(res.begin(), res.end());
  return res;
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;

  vector<vector<bool>> wall_h(h, vector<bool>(w - 1, false));
  vector<vector<bool>> wall_v(h - 1, vector<bool>(w, false));

  for (int i = 0; i < n; ++i) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;

    if (sx == dx) {
      if (sx == 0 || sx == w)
        continue;
      if (sy > dy)
        swap(sy, dy);

      for (int y = sy; y < dy; ++y) {
        wall_h[y][sx - 1] = true;
      }
    } else {
      if (sy == 0 || sy == h)
        continue;
      if (sx > dx)
        swap(sx, dx);

      for (int x = sx; x < dx; ++x) {
        wall_v[sy - 1][x] = true;
      }
    }
  }

  const int V = w * h;
  graph G(V);

  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w - 1; ++x) {
      if (!wall_h[y][x])
        add_edge(G, get_index(x, y, w), get_index(x + 1, y, w));
    }
  }

  for (int y = 0; y < h - 1; ++y) {
    for (int x = 0; x < w; ++x) {
      if (!wall_v[y][x])
        add_edge(G, get_index(x, y, w), get_index(x, y + 1, w));
    }
  }

  int ix, iy, ox, oy;
  cin >> ix >> iy >> ox >> oy;

  const int s = get_index(ix, iy, w);
  const int g = get_index(ox, oy, w);

  vector<int> dist;
  const int base = bfs(s, g, dist, G);
  const auto path = get_path(g, dist, G);

  int ans = 0;
  for (unsigned i = 1; i < path.size(); ++i) {
    chmax(ans, bfs(s, g, dist, G, path[i - 1], path[i]) - base);
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

typedef vector<vector<int>> graph;

inline int get_index(int x, int y, int w) { return x + y * w; }

inline void add_edge(graph &G, int a, int b) {
  G[a].emplace_back(b);
  G[b].emplace_back(a);
}

inline int bfs(int s, int g, vector<int> &dist, const graph &G, int ng_x = -1,
               int ng_y = -1) {
  const int n = G.size();

  dist.assign(n, -1);
  queue<int> que;

  dist[s] = 0;
  que.push(s);

  while (!que.empty()) {
    const int v = que.front();
    que.pop();

    if (v == g)
      return dist[v];

    for (const auto &to : G[v]) {
      if (v == ng_x && to == ng_y)
        continue;

      if (dist[to] == -1) {
        dist[to] = dist[v] + 1;
        que.push(to);
      }
    }
  }

  return -1;
}

vector<int> get_path(int v, const vector<int> &dist, const graph &G) {
  vector<int> res{v};
  while (dist[v] > 0) {
    for (const auto &to : G[v]) {
      if (dist[to] + 1 == dist[v]) {
        v = to;
        res.emplace_back(to);
        break;
      }
    }
  }
  reverse(res.begin(), res.end());
  return res;
}

int main() {
  int w, h, n;
  cin >> w >> h >> n;

  vector<vector<bool>> wall_h(h, vector<bool>(w - 1, false));
  vector<vector<bool>> wall_v(h - 1, vector<bool>(w, false));

  for (int i = 0; i < n; ++i) {
    int sx, sy, dx, dy;
    cin >> sx >> sy >> dx >> dy;

    if (sx == dx) {
      if (sy > dy)
        swap(sy, dy);

      for (int y = sy; y < dy; ++y) {
        wall_h[y][sx - 1] = true;
      }
    } else {
      if (sx > dx)
        swap(sx, dx);

      for (int x = sx; x < dx; ++x) {
        wall_v[sy - 1][x] = true;
      }
    }
  }

  const int V = w * h;
  graph G(V);

  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w - 1; ++x) {
      if (!wall_h[y][x])
        add_edge(G, get_index(x, y, w), get_index(x + 1, y, w));
    }
  }

  for (int y = 0; y < h - 1; ++y) {
    for (int x = 0; x < w; ++x) {
      if (!wall_v[y][x])
        add_edge(G, get_index(x, y, w), get_index(x, y + 1, w));
    }
  }

  int ix, iy, ox, oy;
  cin >> ix >> iy >> ox >> oy;

  const int s = get_index(ix, iy, w);
  const int g = get_index(ox, oy, w);

  vector<int> dist;
  const int base = bfs(s, g, dist, G);
  const auto path = get_path(g, dist, G);

  int ans = 0;
  for (unsigned i = 1; i < path.size(); ++i) {
    chmax(ans, bfs(s, g, dist, G, path[i - 1], path[i]) - base);
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}