結果

問題 No.2897 2集合間距離
ユーザー vjudge1
提出日時 2024-09-25 15:26:34
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 122 ms / 3,500 ms
コード長 849 bytes
コンパイル時間 1,159 ms
コンパイル使用メモリ 92,628 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-09-25 15:26:38
合計ジャッジ時間 3,333 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAXN = 2e5 + 5, MAXV = 1e3 + 3;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

queue<pair<int, int>> q;
int n, m, ans = 3000, a[MAXN], b[MAXN], d[MAXV][MAXV];

void Record(int x, int y, int w) {
  if (x >= 0 && y >= 0 && x <= 1000 && y <= 1000 && !d[x][y]) {
    d[x][y] = w, q.push({x, y});
  }
}

void bfs() {
  while (!q.empty()) {
    auto [x, y] = q.front(); q.pop();
    for (int i = 0; i < 4; i++) {
      Record(x + dx[i], y + dy[i], d[x][y] + 1);
    }
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1, x, y; i <= n; i++) {
    cin >> x >> y, Record(x, y, 1);
  }
  cin >> m, bfs();
  for (int i = 1, x, y; i <= m; i++) {
    cin >> x >> y, ans = min(ans, d[x][y]);
  }
  cout << ans - 1;
  return 0;
}
0