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

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

#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;

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

int main() {
  for (int r; scanf("%d", &r), r;) {
    int w[2], h[2], x[2], y[2];
    static int room[2][500][500];
    static int roomcnt[2][100001];
    for (int k = 0; k < 2; k++) {
      scanf("%d%d%d%d", &w[k], &h[k], &x[k], &y[k]);
      x[k]--, y[k]--;
      for (int i = 0; i < h[k]; i++)
        for (int j = 0; j < w[k]; j++)
          scanf("%d", &room[k][i][j]);

      for (int i = 0; i <= r; i++)
        roomcnt[k][i] = 1 << 30;
      roomcnt[k][0] = 0;

      static bool visited[500][500];
      for (int i = 0; i < h[k]; i++)
        for (int j = 0; j < w[k]; j++)
          visited[i][j] = false;
      visited[y[k]][x[k]] = true;
      int cnt = 0, lev = 0;
      priority_queue<pair<int, pii>> pq;
      pq.push(mp(-room[k][y[k]][x[k]], mp(y[k], x[k])));
      while (!pq.empty()) {
        pair<int, pii> a = pq.top();
        pq.pop();
        int nowlev = -a.first, x = a.second.second, y = a.second.first;
        cnt++;
        if (lev < nowlev)
          lev = nowlev;
        roomcnt[k][cnt] = lev;
        if (cnt == r)
          break;

        for (int i = 0; i < 4; i++) {
          int xx = x + dx[i], yy = y + dy[i];
          if (0 <= xx && xx < w[k] && 0 <= yy && yy < h[k] &&
              !visited[yy][xx]) {
            pq.push(mp(-room[k][yy][xx], mp(yy, xx)));
            visited[yy][xx] = true;
          }
        }
      }
    }

    int ans = 1 << 30;
    for (int i = 0; i <= r; i++)
      ans = min(ans, roomcnt[0][i] + roomcnt[1][r - i]);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 29)
typedef pair<int, int> P;
typedef pair<int, P> F;
priority_queue<F, vector<F>, greater<F>> pq[2];
int xs[] = {-1, 0, 1, 0}, ys[] = {0, 1, 0, -1};
vector<int> input_djk(int z) {
  int w, h, x, y;
  cin >> w >> h >> x >> y;
  x--, y--;
  int dist[w][h], level[w][h];
  bool used[w][h];
  for (int j = 0; j < h; j++) {
    for (int i = 0; i < w; i++) {
      scanf("%d", &level[i][j]);
      dist[i][j] = INF;
      used[i][j] = false;
    }
  }
  dist[x][y] = 1;
  used[x][y] = true;
  pq[z].push(F(1, P(x, y)));
  while (!pq[z].empty()) {
    F tmp = pq[z].top();
    pq[z].pop();
    int tx = tmp.second.first, ty = tmp.second.second;
    if (dist[tx][ty] < tmp.first)
      continue;
    for (int i = 0; i < 4; i++) {
      if (tx + xs[i] < 0 || tx + xs[i] >= w || ty + ys[i] < 0 ||
          ty + ys[i] >= h || used[tx + xs[i]][ty + ys[i]])
        continue;
      tx += xs[i], ty += ys[i];
      if (dist[tx][ty] > max(tmp.first, level[tx][ty])) {
        dist[tx][ty] = max(tmp.first, level[tx][ty]);
        used[tx][ty] = true;
        pq[z].push(F(dist[tx][ty], P(tx, ty)));
      }
      tx -= xs[i], ty -= ys[i];
    }
  }
  vector<int> res;
  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++)
      res.push_back(dist[i][j]);

  sort(res.begin(), res.end());
  return res;
}

int gs(vector<int> x, int r) {
  if (r == 0)
    return 0;
  else
    return x[r - 1];
}

int solve(vector<int> a, vector<int> b, int r) {
  int res = INF;
  for (int i = min(r, (int)a.size()); i >= 0 && b.size() >= r - i; i--) {
    res = min(res, gs(a, i) + gs(b, r - i));
  }
  return res;
}

int main() {
  while (true) {
    int r;
    cin >> r;
    if (r == 0)
      return 0;
    vector<int> a, b;
    a = input_djk(0);
    b = input_djk(1);
    cout << solve(a, b, r) << endl;
  }
}