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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int N = 60;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {1, 0, 0, -1};
bool nt[N][N][4];
int dis[N][N];
int n, m, bx, by, ex, ey;
void bul(int x, int y, int m) {
  if (m) {
    nt[x - 1][y][2] = 0;
    nt[x][y][1] = 0;
  } else {
    nt[x][y - 1][0] = 0;
    nt[x][y][3] = 0;
  }
}
void reb(int x, int y, int m) {
  if (m) {
    nt[x - 1][y][2] = 1;
    nt[x][y][1] = 1;
  } else {
    nt[x][y - 1][0] = 1;
    nt[x][y][3] = 1;
  }
}
int bfs() {
  if (bx == ex && by == ey)
    return 0;
  queue<int> q;
  int x, y, a, b;
  while (!q.empty())
    q.pop();
  memset(dis, 0, sizeof dis);
  q.push((bx << 10) | by);
  dis[bx][by] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    x = u >> 10;
    y = u & 0xff;
    for (int i = 0; i < 4; i++)
      if (nt[x][y][i]) {
        a = x + dx[i];
        b = y + dy[i];
        if (a == ex && b == ey)
          return dis[x][y];
        if (dis[a][b])
          continue;
        dis[a][b] = dis[x][y] + 1;
        q.push((a << 10) | b);
      }
  }
  return -1;
}
int main() {
  // freopen("in.txt", "r", stdin);
  int k;
  while (~scanf("%d%d%d", &n, &m, &k)) {
    memset(nt, 1, sizeof nt);
    for (int i = 0; i < n; i++) {
      nt[i][0][3] = 0;
      nt[i][m - 1][0] = 0;
    }
    for (int j = 0; j < m; j++) {
      nt[0][j][1] = 0;
      nt[n - 1][j][2] = 0;
    }
    while (k--) {
      scanf("%d%d%d%d", &bx, &by, &ex, &ey);
      if (bx == ex) {
        if (by > ey)
          swap(by, ey);
        for (int j = by; j < ey; j++)
          bul(bx, j, 1);
      } else {
        if (bx > ex)
          swap(bx, ex);
        for (int i = bx; i < ex; i++)
          bul(i, by, 0);
      }
    }
    scanf("%d%d%d%d", &bx, &by, &ex, &ey);
    int ans = 0, bg = bfs(), tmp;
    if (bg == -1) {
      puts("0");
      continue;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (nt[i][j][0]) {
          bul(i, j + 1, 0);
          tmp = bfs();
          if (tmp - bg > ans)
            ans = tmp - bg;
          reb(i, j + 1, 0);
        }
        if (nt[i][j][2]) {
          bul(i + 1, j, 1);
          tmp = bfs();
          if (tmp - bg > ans)
            ans = tmp - bg;
          reb(i + 1, j, 1);
        }
      }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int a[2501][2501], d[2501];
int h[2501];
bool bo[2501], Unconect[2501][2501];
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool in(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }
void spfa(int s) {
  for (int i = 0; i <= n * m; ++i) {
    d[i] = 0xfffffff;
  }
  memset(bo, 0, sizeof(bo));
  d[s] = 0;
  bo[s] = 1;
  h[s] = -1;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    bo[now] = 0;
    for (int i = 0; i < n * m; ++i) {
      if (d[i] - a[now][i] > d[now]) {
        d[i] = a[now][i] + d[now];
        h[i] = now;
        if (!bo[i]) {
          bo[i] = 1;
          q.push(i);
        }
      }
    }
  }
}
void spfa2(int s) {
  for (int i = 0; i <= n * m; ++i) {
    d[i] = 0xfffffff;
  }
  memset(bo, 0, sizeof(bo));
  d[s] = 0;
  bo[s] = 1;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    bo[now] = 0;
    for (int i = 0; i < n * m; ++i) {
      if (d[i] - a[now][i] > d[now]) {
        d[i] = a[now][i] + d[now];
        if (!bo[i]) {
          bo[i] = 1;
          q.push(i);
        }
      }
    }
  }
}
int main(int argc, char *argv[]) {
  int N;
  scanf("%d%d%d", &n, &m, &N);
  for (int i = 0; i <= n * m; ++i) {
    for (int j = 0; j <= n * m; ++j) {
      a[i][j] = 0xfffffff;
    }
  }
  for (int i = 0; i < N; ++i) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      for (int j = min(y1, y2); j < max(y1, y2); ++j) {
        int xx = (x1 - 1) * m + j;
        int yy = (x1)*m + j;
        Unconect[xx][yy] = 1;
        Unconect[yy][xx] = 1;
      }
    } else if (y1 == y2) {
      for (int j = min(x1, x2); j < max(x1, x2); ++j) {
        int xx = (j)*m + y1 - 1;
        int yy = j * m + y1;
        Unconect[xx][yy] = 1;
        Unconect[yy][xx] = 1;
      }
    }
  }
  for (int i = 0; i < n * m; ++i) {
    int x = i / m, y = i % m;
    for (int j = 0; j < 4; ++j) {
      int xx = x + dx[j], yy = y + dy[j];
      if (in(xx, yy) && !Unconect[i][xx * m + yy]) {
        a[i][xx * m + yy] = 1;
        a[xx * m + yy][i] = 1;
      }
    }
  }
  int x, y;
  scanf("%d%d", &x, &y);
  int s = x * m + y;
  scanf("%d%d", &x, &y);
  int t = x * m + y;
  spfa(s);
  int sb = d[t];
  x = t;
  y = h[x];
  int ans = 0;
  while (y != -1) {
    a[x][y] = 0xfffffff;
    a[y][x] = 0xfffffff;
    spfa2(s);
    if (d[t] < 0xfffffff)
      ans = max(ans, d[t]);
    a[x][y] = 1;
    a[y][x] = 1;
    x = y;
    y = h[x];
  }
  if (ans == 0)
    cout << 0 << endl;
  else
    cout << ans - sb << endl;
  return 0;
}