結果
| 問題 |
No.2354 Poor Sight in Winter
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-19 00:12:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,228 bytes |
| コンパイル時間 | 2,776 ms |
| コンパイル使用メモリ | 224,088 KB |
| 最終ジャッジ日時 | 2025-02-14 22:47:34 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 WA * 2 |
ソースコード
typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long
ll isqrt(ll N){
ll sqrtN=sqrt(N)-1;
while(sqrtN+1<=N/(sqrtN+1))sqrtN++;
return sqrtN;
}
// Union-Find
struct UnionFind {
// core member
vector<int> par;
// constructor
UnionFind() { }
UnionFind(int n) : par(n, -1) { }
void init(int n) { par.assign(n, -1); }
// core methods
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x), y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
// debug
friend ostream& operator << (ostream &s, UnionFind uf) {
map<int, vector<int>> groups;
for (int i = 0; i < uf.par.size(); ++i) {
int r = uf.root(i);
groups[r].push_back(i);
}
for (const auto &it : groups) {
s << "group: ";
for (auto v : it.second) s << v << " ";
s << endl;
}
return s;
}
};
signed main(){
ll n,k;
std::cin >> n>>k;
pair<ll,ll> s,g;
std::cin >> s.first>>s.second>>g.first>>g.second;
vector<ll> x(n),y(n);
for (int i = 0; i < n; i++) {
std::cin >> x[i]>>y[i];
}
ll r = 2*1e7;
ll l = 0;
while(r-l>1){
ll m =(r+l)/2;
// std::cout << l<<" "<<r << std::endl;
UnionFind uf(n+2);
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if(abs(x[i]-x[j])+abs(y[i]-y[j])<=m){
uf.merge(i,j);
}
}
}
for (int i = 0; i < n; i++) {
if(abs(x[i]-s.first)+abs(y[i]-s.second)<=m){
uf.merge(i,n);
}
if(abs(x[i]-g.first)+abs(y[i]-g.second)<=m){
uf.merge(i,n+1);
}
}
vector<vector<ll>> dis(n+2,vector<ll>(n+2,1e10));
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if(uf.same(i,j))dis[uf.root(i)][uf.root(i)]=0;
if(!uf.same(i,j)){
ll d = abs(x[i]-x[j])+abs(y[i]-y[j]);
dis[uf.root(i)][uf.root(j)] = min(dis[uf.root(i)][uf.root(j)], (d+m-1)/(m)-1);
dis[uf.root(j)][uf.root(i)] = min(dis[uf.root(j)][uf.root(i)], (d+m-1)/(m)-1);
}
}
}
for (int i = 0; i < n; i++) {
if(!uf.same(i,n)){
ll d = abs(x[i]-s.first)+abs(y[i]-s.second);
dis[uf.root(i)][uf.root(n)] = min(dis[uf.root(i)][uf.root(n)], (d+m-1)/(m)-1);
dis[uf.root(n)][uf.root(i)] = min(dis[uf.root(n)][uf.root(i)], (d+m-1)/(m)-1);
}
if(!uf.same(i,n+1)){
ll d = abs(x[i]-g.first)+abs(y[i]-g.second);
dis[uf.root(i)][uf.root(n+1)] = min(dis[uf.root(i)][uf.root(n+1)], (d+m-1)/(m)-1);
dis[uf.root(n+1)][uf.root(i)] = min(dis[uf.root(n+1)][uf.root(i)], (d+m-1)/(m)-1);
}
}
set<ll> uni;
for (int i = 0; i < n; i++) {
uni.insert(uf.root(i));
}
uni.insert(uf.root(n));
uni.insert(uf.root(n+1));
vector<ll> dd(n+2,1e10);
dd[uf.root(n)]=0;
using P = pair<ll,ll>;
priority_queue<P,vector<P>,greater<P>> pq;
pq.push({0, uf.root(n)});
while(!pq.empty()){
auto [dist, pos] = pq.top();pq.pop();
if(dist>dd[pos])continue;
for (auto e : uni) {
if(e==pos)continue;
if(dd[e]>dist+dis[pos][e]){
dd[e] = dist+dis[pos][e];
pq.push({dd[e], e});
}
}
}
if(dd[uf.root(n+1)]<=k){
r = m;
}else{
l = m;
}
}
std::cout << r << std::endl;
}