結果

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

const int N = 2e5 + 10, M = 1e3 + 10;

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

int n, x[N], y[N], m, z[N], w[N], d[M][M], ans = 2 * M;
queue<pii> que;

void Record(int x, int y, int dis) {
    if (x < 0 || x >= M || y < 0 || y >= M || d[x][y]) return ;
    d[x][y] = dis, que.push({x, y});
}

void bfs() {
    for (int i = 1; i <= n; i++) Record(x[i], y[i], 1);
    while (!que.empty()) {
        auto [x, y] = que.front(); que.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; i <= n; i++) cin >> x[i] >> y[i];
    cin >> m;
    for (int i = 1; i <= m; i++) cin >> z[i] >> w[i];
    bfs();
    for (int i = 1; i <= m; i++) ans = min(ans, d[z[i]][w[i]] - 1);
    cout << ans;
    return 0;
}
0