結果

問題 No.3 ビットすごろく
ユーザー kibou1136
提出日時 2021-12-08 17:23:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,213 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 181,352 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-16 08:06:06
合計ジャッジ時間 2,606 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Edge{
    int to; long long w;
    Edge(int to, long long w): to(to), w(w){}
};

using Gragh = vector<vector<Edge>>;
Gragh G;
int n;
const int INF = 10800000;

template<class T> bool chmin(T &a, T b){
    if (b < a){
        a = b;
        return true;
    }else{
        return false;
    }
}





int main(){
    cin >> n;
    G.resize(n + 1);
    for (int i = 1; i <= n; ++i){
        int digi = __builtin_popcount(i);
        if (i - digi >= 1) G[i].push_back(Edge(i - digi,1));
        if (i + digi <= n) G[i].push_back(Edge(i + digi,1));
    }
    vector<long long> dist(n + 1, INF);
    dist[1] = 1;
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que;
    que.push(make_pair(dist[1], 1));
    while (!que.empty()){
        int cur = que.top().second;
        long long d = que.top().first;
        que.pop();
        if (dist[cur] < d) continue;
        for (auto e: G[cur]){
            if (chmin(dist[e.to], dist[cur] + e.w)){
                que.push(make_pair(dist[e.to], e.to));
            }
        }
    }
    if (dist[n] < INF) cout << dist[n] << endl;
    else cout << -1 << endl;
    
}
0