結果

問題 No.2354 Poor Sight in Winter
ユーザー erbowlerbowl
提出日時 2023-06-19 00:12:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,228 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 230,200 KB
実行使用メモリ 5,508 KB
最終ジャッジ日時 2023-09-08 17:47:45
合計ジャッジ時間 5,084 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 56 ms
5,308 KB
testcase_13 AC 71 ms
5,436 KB
testcase_14 AC 83 ms
5,436 KB
testcase_15 AC 90 ms
5,372 KB
testcase_16 AC 82 ms
5,508 KB
testcase_17 AC 76 ms
5,364 KB
testcase_18 AC 27 ms
4,376 KB
testcase_19 AC 58 ms
4,784 KB
testcase_20 AC 38 ms
4,376 KB
testcase_21 WA -
testcase_22 AC 18 ms
4,380 KB
testcase_23 AC 19 ms
4,376 KB
testcase_24 AC 50 ms
4,456 KB
testcase_25 AC 14 ms
4,376 KB
testcase_26 AC 10 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
}


0