結果

問題 No.2897 2集合間距離
ユーザー n0ma_ru
提出日時 2024-09-21 15:40:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 250 ms / 3,500 ms
コード長 1,069 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 205,380 KB
最終ジャッジ日時 2025-02-24 11:16:54
ジャッジサーバーID
(参考情報)
judge2 / 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;
#define MAX (1000)
int N,M;
vector<pair<int,int>> P,Q;
int main() {
    cin >> N;
    P.resize(N);
    for (auto& [x,y] : P) cin >> x >> y;
    cin >> M;
    Q.resize(M);
    for (auto& [x,y] : Q) cin >> x >> y;

    vector<vector<int>> dis(MAX, vector<int>(MAX,1e9));
    queue<pair<int,int>> q;
    for (auto [x,y] : P) {
        q.emplace(x,y);
        dis[y][x] = 0;
    }
    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 < MAX && 0 <= ny && ny < MAX)) 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] : Q) {
        ans = min(ans, dis[y][x]);
    }
    cout << ans << '\n';
}
0