Problem p00287 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;
typedef pair<int, int> P;
struct data {
  int x, y, c;
  data(int xx, int yy, int cc) {
    x = xx;
    y = yy;
    c = cc;
  }
};
int w, h, m, q;
int px[101], py[101], qx[101], qy[101];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
vector<int> xid, yid;
vector<int> edge[15001];
int fie[2001][2001];
int dp[15001];
int cnt;

int sx[101], sy[101], gx[101], gy[101];

int bfs(int x, int y) {
  queue<P> que;
  que.push(P(x, y));
  fie[x][y] = cnt;
  while (que.size()) {
    P p = que.front();
    que.pop();
    int xx = p.first, yy = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = xx + dx[i], ny = yy + dy[i];
      if (fie[nx][ny] == 0) {
        fie[nx][ny] = cnt;
        que.push(P(nx, ny));
      }
    }
  }
}

int bfs2(int f, int t) {
  memset(dp, -1, sizeof(dp));
  queue<int> que;
  dp[f] = 0;
  que.push(f);
  while (que.size()) {
    int v = que.front();
    que.pop();
    if (v == t)
      return dp[t];
    for (int i = 0; i < edge[v].size(); i++) {
      int next = edge[v][i];
      if (dp[next] == -1) {
        dp[next] = dp[v] + 1;
        que.push(next);
      }
    }
  }
  return -1;
}

int main(void) {
  scanf("%d%d%d", &w, &h, &m);
  xid.push_back(0);
  xid.push_back(w * 2);
  yid.push_back(0);
  yid.push_back(h * 2);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d%d", &px[i], &py[i], &qx[i], &qy[i]);
    px[i] *= 2;
    py[i] *= 2;
    qx[i] *= 2;
    qy[i] *= 2;
    if (px[i] > qx[i])
      swap(px[i], qx[i]);
    if (py[i] > qy[i])
      swap(py[i], qy[i]);
    if (px[i] > 0)
      xid.push_back(px[i] - 1);
    if (qx[i] > 0)
      xid.push_back(qx[i] - 1);
    if (py[i] > 0)
      yid.push_back(py[i] - 1);
    if (qy[i] > 0)
      yid.push_back(qy[i] - 1);
    xid.push_back(px[i]);
    xid.push_back(qx[i]);
    yid.push_back(py[i]);
    yid.push_back(qy[i]);
    if (px[i] + 1 < w * 2)
      xid.push_back(px[i] + 1);
    if (qx[i] + 1 < w * 2)
      xid.push_back(qx[i] + 1);
    if (py[i] + 1 < h * 2)
      yid.push_back(py[i] + 1);
    if (qy[i] + 1 < h * 2)
      yid.push_back(qy[i] + 1);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &sx[i], &sy[i], &gx[i], &gy[i]);
    sx[i] = sx[i] * 2;
    sy[i] = sy[i] * 2;
    gx[i] = gx[i] * 2;
    gy[i] = gy[i] * 2;
    xid.push_back(sx[i]);
    xid.push_back(gx[i]);
    yid.push_back(sy[i]);
    yid.push_back(gy[i]);
  }
  sort(xid.begin(), xid.end());
  xid.erase(unique(xid.begin(), xid.end()), xid.end());
  sort(yid.begin(), yid.end());
  yid.erase(unique(yid.begin(), yid.end()), yid.end());
  for (int i = 0; i < m; i++) {
    int num = lower_bound(xid.begin(), xid.end(), px[i]) - xid.begin();
    px[i] = num;
    num = lower_bound(xid.begin(), xid.end(), qx[i]) - xid.begin();
    qx[i] = num;
    num = lower_bound(yid.begin(), yid.end(), py[i]) - yid.begin();
    py[i] = num;
    num = lower_bound(yid.begin(), yid.end(), qy[i]) - yid.begin();
    qy[i] = num;
  }
  for (int i = 0; i < q; i++) {
    int num = lower_bound(xid.begin(), xid.end(), sx[i]) - xid.begin();
    sx[i] = num;
    num = lower_bound(xid.begin(), xid.end(), gx[i]) - xid.begin();
    gx[i] = num;
    num = lower_bound(yid.begin(), yid.end(), sy[i]) - yid.begin();
    sy[i] = num;
    num = lower_bound(yid.begin(), yid.end(), gy[i]) - yid.begin();
    gy[i] = num;
  }
  memset(fie, -1, sizeof(fie));
  for (int i = 1; i < xid.size() - 1; i++) {
    for (int j = 1; j < yid.size() - 1; j++) {
      fie[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = px[i]; j <= qx[i]; j++) {
      fie[j][py[i]] = -1;
      fie[j][qy[i]] = -1;
    }
    for (int j = py[i]; j <= qy[i]; j++) {
      fie[px[i]][j] = -1;
      fie[qx[i]][j] = -1;
    }
  }
  for (int i = 0; i < yid.size(); i++) {
    for (int j = 0; j < xid.size(); j++) {
      if (fie[j][i] == 0) {
        cnt++;
        bfs(j, i);
      }
    }
  }
  for (int i = 0; i < yid.size(); i++) {
    for (int j = 0; j < xid.size(); j++) {
      if (fie[j][i] >= 1) {
        for (int k = 0; k < 4; k++) {
          int nx = j + dx[k] * 2, ny = i + dy[k] * 2;
          if (nx >= 0 && nx < xid.size() && ny >= 0 && ny < yid.size()) {
            if (fie[nx][ny] >= 0 && fie[nx][ny] != fie[j][i]) {
              int l;
              for (l = 0; l < edge[fie[j][i]].size(); l++) {
                if (edge[fie[j][i]][l] == fie[nx][ny])
                  break;
              }
              if (l == edge[fie[j][i]].size()) {
                edge[fie[j][i]].push_back(fie[nx][ny]);
                edge[fie[nx][ny]].push_back(fie[j][i]);
              }
            }
          }
        }
      }
    }
  }
  /*
  for(int i=0;i<yid.size();i++){
          for(int j=0;j<xid.size();j++){
                  printf("%2d%c",fie[j][i],j+1==xid.size()?'\\n':' ');
          }
  }
  */
  for (int i = 0; i < q; i++)
    printf("%d\\n", bfs2(fie[sx[i]][sy[i]], fie[gx[i]][gy[i]]));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;
typedef pair<int, int> P;
struct data {
  int x, y, c;
  data(int xx, int yy, int cc) {
    x = xx;
    y = yy;
    c = cc;
  }
};
int w, h, m, q;
int px[101], py[101], qx[101], qy[101];
int sx[101], sy[101], gx[101], gy[101];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
vector<int> xid, yid;
int fie[801][801];
int used[801][801];

int dp[801][801];

int bfs(int sx, int sy, int gx, int gy) {
  memset(dp, -1, sizeof(dp));
  queue<data> que;
  que.push(data(sx, sy, 0));
  dp[sx][sy] = 0;
  while (que.size()) {
    data d = que.front();
    que.pop();
    int x = d.x, y = d.y, c = d.c;
    if (dp[x][y] < c)
      continue;
    queue<P> que2;
    que2.push(P(x, y));
    while (que2.size()) {
      P p = que2.front();
      que2.pop();
      int xx = p.first, yy = p.second;
      for (int i = 0; i < 4; i++) {
        int nx = xx + dx[i], ny = yy + dy[i];
        if (fie[nx][ny] == -1)
          continue;
        if (fie[nx][ny] == 0 && dp[nx][ny] == -1) {
          dp[nx][ny] = c;
          que2.push(P(nx, ny));
        }
      }
    }
    memset(used, 0, sizeof(used));
    que2.push(P(x, y));
    used[x][y] = 1;
    while (que2.size()) {
      P p = que2.front();
      que2.pop();
      int xx = p.first, yy = p.second;
      for (int i = 0; i < 4; i++) {
        int nx = xx + dx[i], ny = yy + dy[i];
        if (fie[nx][ny] == -1)
          continue;
        if (fie[nx][ny] == 0 && used[nx][ny] == 0) {
          used[nx][ny] = 1;
          que2.push(P(nx, ny));
        }
        if (fie[nx][ny] == 1) {
          nx += dx[i];
          ny += dy[i];
          if (fie[nx][ny] == 0 && dp[nx][ny] == -1) {
            dp[nx][ny] = c + 1;
            que.push(data(nx, ny, c + 1));
          }
        }
      }
    }
  }
  return dp[gx][gy];
}

int main(void) {
  scanf("%d%d%d", &w, &h, &m);
  xid.push_back(0);
  xid.push_back(w * 2);
  yid.push_back(0);
  yid.push_back(h * 2);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d%d", &px[i], &py[i], &qx[i], &qy[i]);
    px[i] *= 2;
    py[i] *= 2;
    qx[i] *= 2;
    qy[i] *= 2;
    xid.push_back(px[i] - 1);
    xid.push_back(qx[i] - 1);
    yid.push_back(py[i] - 1);
    yid.push_back(qy[i] - 1);
    xid.push_back(px[i]);
    xid.push_back(qx[i]);
    yid.push_back(py[i]);
    yid.push_back(qy[i]);
    xid.push_back(px[i] + 1);
    xid.push_back(qx[i] + 1);
    yid.push_back(py[i] + 1);
    yid.push_back(qy[i] + 1);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &sx[i], &sy[i], &gx[i], &gy[i]);
    sx[i] *= 2;
    sy[i] *= 2;
    gx[i] *= 2;
    gy[i] *= 2;
    xid.push_back(sx[i]);
    xid.push_back(gx[i]);
    yid.push_back(sy[i]);
    yid.push_back(gy[i]);
  }
  sort(xid.begin(), xid.end());
  xid.erase(unique(xid.begin(), xid.end()), xid.end());
  sort(yid.begin(), yid.end());
  yid.erase(unique(yid.begin(), yid.end()), yid.end());
  for (int i = 0; i < m; i++) {
    int num = lower_bound(xid.begin(), xid.end(), px[i]) - xid.begin();
    px[i] = num;
    num = lower_bound(xid.begin(), xid.end(), qx[i]) - xid.begin();
    qx[i] = num;
    num = lower_bound(yid.begin(), yid.end(), py[i]) - yid.begin();
    py[i] = num;
    num = lower_bound(yid.begin(), yid.end(), qy[i]) - yid.begin();
    qy[i] = num;
  }
  for (int i = 0; i < q; i++) {
    int num = lower_bound(xid.begin(), xid.end(), sx[i]) - xid.begin();
    sx[i] = num;
    num = lower_bound(xid.begin(), xid.end(), gx[i]) - xid.begin();
    gx[i] = num;
    num = lower_bound(yid.begin(), yid.end(), sy[i]) - yid.begin();
    sy[i] = num;
    num = lower_bound(yid.begin(), yid.end(), gy[i]) - yid.begin();
    gy[i] = num;
  }
  memset(fie, -1, sizeof(fie));
  for (int i = 1; i < xid.size() - 1; i++) {
    for (int j = 1; j < yid.size() - 1; j++) {
      fie[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = px[i]; j <= qx[i]; j++) {
      fie[j][py[i]] = 1;
      fie[j][qy[i]] = 1;
    }
    for (int j = py[i]; j <= qy[i]; j++) {
      fie[px[i]][j] = 1;
      fie[qx[i]][j] = 1;
    }
  }
  /*
  for(int i=0;i<yid.size();i++){
          for(int j=0;j<xid.size();j++){
                  printf("%d%c",fie[j][i],j==xid.size()-1?'\\n':' ');
          }
  }
  */
  for (int i = 0; i < q; i++)
    printf("%d\\n", bfs(sx[i], sy[i], gx[i], gy[i]));
  return 0;
}