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

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int x[2], y[2], w[2], h[2];
int R;
vector<vvi> zimu;

void dfs(int i, int x, int y, vvi &used, vector<pair<int, int>> &list) {

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      que;
  que.push(pair<int, pii>(1, pii(x, y)));

  int auth = 0;
  int room = 0;
  while (que.size()) {
    int corr_lev = que.top().first;
    int corr_x = que.top().second.first;
    int corr_y = que.top().second.second;
    // cout << "POP (" << corr_x << "," << corr_y <<") " << corr_lev << endl;
    que.pop();
    if (used[corr_y][corr_x] == 1)
      continue;
    used[corr_y][corr_x] = 1;

    if (corr_lev > auth) {
      list.push_back(pii(room, auth));
      auth = corr_lev;
    }
    room++;
    if (room == w[i] * h[i])
      break;

    for (int j = 0; j < 4; j++) {
      int nx = corr_x + dx[j];
      int ny = corr_y + dy[j];
      if (nx >= w[i] || nx < 0 || ny >= h[i] || ny < 0)
        continue; // ????£?
      if (used[ny][nx] == 1)
        continue; // ??¢?´¢??????
      que.push(pair<int, pii>(zimu[i][ny][nx], pii(nx, ny)));
      // cout << "  PUSH (" << nx << "," << ny << ") " << zimu[i][ny][nx] <<
      // endl;
    }
  }
  list.push_back(pii(room, auth));
}

int main() {
  while (1) {

    cin >> R;
    if (!R)
      break;
    zimu.clear();

    // ??\\???
    for (int i = 0; i < 2; i++) {
      cin >> w[i] >> h[i] >> x[i] >> y[i];

      zimu.push_back(vvi(h[i], vi(w[i])));

      for (int j = 0; j < h[i]; j++)
        for (int k = 0; k < w[i]; k++)
          cin >> zimu[i][j][k];
    }

    //
    vector<vector<pair<int, int>>> list(
        2); // ???????????¨?±???°???????¨???¬??????

    for (int i = 0; i < 2; i++) {
      list[i].push_back(pii(0, 0));
      vvi used(h[i], vi(w[i], 0));
      dfs(i, x[i] - 1, y[i] - 1, used, list[i]);
    }
    /*
    for(int i=0; i<2; i++){
      for(int j=0; j<list[i].size(); j++)
        cout << "(" << list[i][j].first << "," << list[i][j].second << "), ";
      cout << endl;
      }*/

    int ans = 1e+9;
    for (int i = 0; i < list[0].size(); i++) {
      int rooma = list[0][i].first;
      int autha = list[0][i].second;
      pii tmp = *lower_bound(list[1].begin(), list[1].end(), pii(R - rooma, 0));
      int roomb = tmp.first;
      int authb = tmp.second;
      if (rooma + roomb < R)
        continue;
      ans = min(ans, autha + authb);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int x[2], y[2], w[2], h[2];
int R;
vector<vvi> zimu;

void dfs(int i, int x, int y, int &lev, vvi &used, int *mini, int *room) {
  // cout << " " << x << " " << y << endl;
  used[y][x] = 1;
  (*room)++;
  for (int j = 0; j < 4; j++) {
    int nx = x + dx[j];
    int ny = y + dy[j];
    if (nx >= w[i] || nx < 0 || ny >= h[i] || ny < 0)
      continue; // ????£?
    if (used[ny][nx] == 1)
      continue;                  // ??¢?´¢??????
    if (zimu[i][ny][nx] > lev) { // ????¨???¬???????¶??????????
      *mini = min(*mini, zimu[i][ny][nx]);
      continue;
    }
    dfs(i, nx, ny, lev, used, mini, room);
  }
}

int main() {
  while (1) {

    cin >> R;
    if (!R)
      break;
    zimu.clear();

    // ??\\???
    for (int i = 0; i < 2; i++) {
      cin >> w[i] >> h[i] >> x[i] >> y[i];

      zimu.push_back(vvi(h[i], vi(w[i])));

      for (int j = 0; j < h[i]; j++)
        for (int k = 0; k < w[i]; k++)
          cin >> zimu[i][j][k];
    }

    //
    vector<vector<pair<int, int>>> list(
        2); // ???????????¨?±???°???????¨???¬??????

    for (int i = 0; i < 2; i++) {
      list[i].push_back(pii(0, 0));
      int lev = 1;
      int mini = 1e+9;
      int room = 0;
      do {
        room = 0;
        vvi used(h[i], vi(w[i], 0));
        dfs(i, x[i] - 1, y[i] - 1, lev, used, &mini, &room);
        // cout << room << " "<< lev << endl;
        list[i].push_back(pii(room, lev));
        lev = mini;
        mini = 1e+9;
      } while (room !=
               w[i] *
                   h[i]); // ??????????????¨?±?????????????????¨???¬???????????§
    }
    /*
    for(int i=0; i<2; i++){
      for(int j=0; j<list[i].size(); j++)
        cout << "(" << list[i][j].first << "," << list[i][j].second << "), ";
      cout << endl;
    }
    */

    int ans = 1e+9;
    for (int i = 0; i < list[0].size(); i++) {
      int rooma = list[0][i].first;
      int autha = list[0][i].second;
      pii tmp = *lower_bound(list[1].begin(), list[1].end(), pii(R - rooma, 0));
      int roomb = tmp.first;
      int authb = tmp.second;
      if (rooma + roomb < R)
        continue;
      ans = min(ans, autha + authb);
    }
    cout << ans << endl;
  }
}