結果
| 問題 | 
                            No.2897 2集合間距離
                             | 
                    
| コンテスト | |
| ユーザー | 
                             vjudge1
                         | 
                    
| 提出日時 | 2024-09-25 15:41:49 | 
| 言語 | C++17(clang)  (17.0.6 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 108 ms / 3,500 ms | 
| コード長 | 908 bytes | 
| コンパイル時間 | 2,743 ms | 
| コンパイル使用メモリ | 163,388 KB | 
| 実行使用メモリ | 11,768 KB | 
| 最終ジャッジ日時 | 2024-09-25 15:41:54 | 
| 合計ジャッジ時間 | 4,897 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 24 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAXN = 200001, MAXV = 1000, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, m, dist[MAXV][MAXV], ans = int(1e9);
bool vis[MAXV][MAXV];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  queue<pii> que;
  for(int i = 1, x, y; i <= n; ++i) {
    cin >> x >> y;
    vis[x][y] = 1;
    dist[x][y] = 0;
    que.push({x, y});
  }
  for(; !que.empty(); ) {
    auto [x, y] = que.front();
    que.pop();
    for(int d : {0, 1, 2, 3}) {
      int nx = x + dx[d], ny = y + dy[d];
      if(nx >= 0 && nx < 1000 && ny >= 0 && ny < 1000 && !vis[nx][ny]) {
        vis[nx][ny] = 1;
        dist[nx][ny] = dist[x][y] + 1;
        que.push({nx, ny});
      }
    }
  }
  cin >> m;
  for(int i = 1, x, y; i <= m; ++i) {
    cin >> x >> y;
    ans = min(ans, dist[x][y]);
  }
  cout << ans;
  return 0;
}
            
            
            
        
            
vjudge1