結果

問題 No.2179 Planet Traveler
ユーザー MasKoaTS
提出日時 2024-12-03 16:23:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 84 ms / 3,000 ms
コード長 1,701 bytes
コンパイル時間 2,108 ms
コンパイル使用メモリ 210,720 KB
最終ジャッジ日時 2025-02-26 10:47:54
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define INF 4611686018427387903ll
using namespace std;
using ll = long long;
template <class T>  using PQ = priority_queue<T, vector<T>, greater<T> >;

ll isqrt(ll n) {
    ll ok = 0ll;
    ll ng = 2000000000ll;
    while(abs(ok - ng) > 1){
        ll mid = (ok + ng) / 2;
        if(mid * mid <= n){
            ok = mid;
        }
        else{
            ng = mid;
        }
    }
    return ok;
}

ll pow_ll(ll a, int b){
    ll ret = 1;
    for(int i = 0; i < b; ++i){
        ret *= a;
    }
    return ret;
}


int main(){
    int n;  cin >> n;
    vector<tuple<ll, ll, int> > planets(n);
    for(auto&[x, y, z] : planets){
        cin >> x >> y >> z;
    }

    vector<vector<pair<int, ll> > > graph(n, vector<pair<int, ll> >({}));
    for(int i = 0; i < n - 1; ++i){
        auto&[x1, y1, t1] = planets[i];
        for(int j = i + 1; j < n; ++j){
            auto&[x2, y2, t2] = planets[j];
            ll d = pow_ll(x1 - x2, 2) + pow_ll(y1 - y2, 2);
            if(t1 != t2){
                ll r1 = x1 * x1 + y1 * y1;
                ll r2 = x2 * x2 + y2 * y2;
                d = r1 + r2 - isqrt(4 * r1 * r2);
            }
            graph[i].push_back({j, d});
            graph[j].push_back({i, d});
        }
    }

    vector<ll> dp(n, INF);
    PQ<pair<ll, int> > que = {};
    que.push({0, 0});
    while(!que.empty()){
        auto[c, v] = que.top();    que.pop();
        if(dp[v] <= c){
            continue;
        }
        dp[v] = c;
        if(v == n - 1){
            break;
        }
        for(auto&[nv, nd] : graph[v]){
            que.push({max(c, nd), nv});
        }
    }
    ll ans = dp[n - 1];
    cout << ans << endl;

    return 0;
}
0