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

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

using namespace std;

typedef pair<int, pair<int, int>> P;

const int INF = 1 << 30;

int R, H[2], W[2], X[2], Y[2];
int G[2][512][512];
int dy[] = {0, -1, 0, 1}, dx[] = {-1, 0, 1, 0};
int L[2][100005];

void C(int h, int w, int x, int y, int l[100005], int g[512][512]) {

  int vis[512][512];
  memset(vis, -1, sizeof vis);
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(make_pair(1, make_pair(y, x)));
  vis[y][x] = 0;
  for (int i = 0; i <= R; i++)
    l[i] = INF;
  l[0] = min(l[0], 0);

  for (int i = 1; i <= R && i <= h * w; i++) {
    P p = q.top();
    q.pop();
    l[i] = min(l[i], p.first);
    int cy = p.second.first, cx = p.second.second;
    for (int d = 0; d < 4; d++) {
      int ty = cy + dy[d], tx = cx + dx[d];
      if (ty < 0 || ty >= h || tx < 0 || tx >= w || vis[ty][tx] != -1)
        continue;
      vis[ty][tx] = 0;
      //      cout << g[ty][tx] << endl;
      q.push(make_pair(max(g[ty][tx], l[i]), make_pair(ty, tx)));
    }
  }
}

int main() {
  while (scanf("%d", &R) && R) {
    for (int i = 0; i < 2; i++) {
      scanf("%d%d%d%d", &W[i], &H[i], &X[i], &Y[i]);
      X[i]--;
      Y[i]--;
      for (int j = 0; j < H[i]; j++)
        for (int k = 0; k < W[i]; k++)
          scanf("%d", &G[i][j][k]);
    }

    for (int i = 0; i < 2; i++) {
      C(H[i], W[i], X[i], Y[i], L[i], G[i]);
      //      for(int j=0;j<=R;j++) printf("%d ", L[i][j]); puts("");
    }

    int res = INF;
    for (int i = 0; i <= R; i++)
      res = min(res, L[0][i] + L[1][R - i]);
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 30;

int R, H[2], W[2], X[2], Y[2];
int G[2][512][512];
int dy[] = {0, -1, 0, 1}, dx[] = {-1, 0, 1, 0};

int C(int d, int r, int h, int w, int x, int y, int g[512][512]) {
  int res = 0, vis[512][512];
  memset(vis, -1, sizeof vis);
  queue<P> q;
  q.push(P(y, x));
  vis[y][x] = 0;
  //  cout << "tansaku  "<< y << " " << x << " " << g[y][x] << endl;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (g[p.first][p.second] > d)
      continue;
    res++;
    for (int i = 0; i < 4; i++) {
      int ny = dy[i] + p.first, nx = dx[i] + p.second;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || vis[ny][nx] != -1)
        continue;
      vis[ny][nx] = vis[p.first][p.second] + 1;
      q.push(P(ny, nx));
    }
  }
  return res >= r;
}

int main() {
  while (scanf("%d", &R) && R) {
    for (int i = 0; i < 2; i++) {
      scanf("%d%d%d%d", &W[i], &H[i], &X[i], &Y[i]);
      X[i]--;
      Y[i]--;
      for (int j = 0; j < H[i]; j++)
        for (int k = 0; k < W[i]; k++)
          scanf("%d", &G[i][j][k]);
    }
    //    puts("END OF FILE");
    int res = INF;
    for (int x = 0; x <= R; x++) {
      //      if(x!=3) continue;
      int r[2];
      r[0] = x;
      r[1] = R - x;
      int sum = 0;
      for (int i = 0; i < 2; i++) {
        int lb = -1, ub = 100000005;
        while (lb + 1 < ub) {
          int mid = (lb + ub) / 2;
          //	  cout << lb << " " << ub << endl;
          if (C(mid, r[i], H[i], W[i], X[i], Y[i], G[i]))
            ub = mid;
          else
            lb = mid;
        }
        sum += ub;
        ///	cout << r[0] << " " << r[1] << " " << sum << endl;
      }
      res = min(res, sum);
    }
    printf("%d\\n", res);
  }
  return 0;
}