結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
int n,m;
int siz = 1e3;
vector<pair<int,int>> xy, wz;
int main() {
    cin >> n;
    xy.resize(n);
    for (auto& [x,y] : xy) {
        cin >> x >> y;
    }
    cin >> m;
    wz.resize(m);
    for (auto& [x,y] : wz) {
        cin >> x >> y;
    }
    vector<vector<int>> dis(siz, vector<int>(siz, 1e9));
    queue<pair<int,int>> q;
    for (auto [x,y] : xy) {
        dis[y][x] = 0;
        q.emplace(x,y);
    }
    int dx[] = {-1,0,1,0,-1};
    while (q.size()) {
        auto [x,y] = q.front();
        q.pop();
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i];
            int ny = y + dx[i+1];
            if (!(0 <= nx && nx < siz && 0 <= ny && ny < siz)) continue;
            if (dis[ny][nx] > dis[y][x] + 1) {
                dis[ny][nx] = dis[y][x] + 1;
                q.emplace(nx, ny);
            }
        }
    }
    int ans = 1e9;
    for (auto [x,y] : wz) {
        ans = min(ans, dis[y][x]);
    }
    cout << ans << '\n';
}
0