結果
問題 |
No.2897 2集合間距離
|
ユーザー |
|
提出日時 | 2024-09-20 23:21:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 120 ms / 3,500 ms |
コード長 | 965 bytes |
コンパイル時間 | 2,090 ms |
コンパイル使用メモリ | 202,304 KB |
最終ジャッジ日時 | 2025-02-24 10:44:01 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 24 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; queue<pair<int,int>> que; constexpr int r = 1000; vector<vector<int>> A(r, vector<int>(r, 1 << 30)); for(int i = 0; i < n; i++){ int y, x; cin >> y >> x; A[y][x] = 0; que.emplace(y, x); } while(!que.empty()){ auto [y, x] = que.front(); que.pop(); for(int i = 0; i < 4; i++){ int ny = y + (i == 0) - (i == 1); int nx = x + (i == 2) - (i == 3); if(ny < 0 || nx < 0 || ny >= r || nx >= r) continue; if(A[y][x] + 1 >= A[ny][nx])continue; A[ny][nx] = A[y][x] + 1; que.emplace(ny, nx); } } int m, ans = 1 << 30; cin >> m; while(m--){ int y, x; cin >> y >> x; ans = min(ans, A[y][x]); } cout << ans << '\n'; }