結果

問題 No.3 ビットすごろく
ユーザー kibou1136kibou1136
提出日時 2021-12-08 17:23:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,213 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 177,480 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 08:16:50
合計ジャッジ時間 3,105 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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