結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 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,380 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,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 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<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
#include<chrono>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define vint(a,n) vint a(n); rep(i, n) cin >> a[i];
#define vll(a,n) vll a(n); rep(i, n) cin >> a[i];
#define ALL(n) begin(n),end(n)
#define RALL(n) rbegin(n),rend(n)
#define MOD (1000000007)
// #define MOD (998244353)
#define INF (1e9+7)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint=vector<int>;
using vll=vector<ll>;
using vbool=vector<bool>;
using P=pair<ll,ll>;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pl(T x){cout << x << " ";}
template<class T>void pr(T x){cout << x << endl;}
template <class T, class... Ts> inline void pr(T Tar, Ts... ts) { std::cout << Tar << " "; pr(ts...); return; }
template<class T>void prvec(vector<T>& a){rep(i, (int)a.size()-1){pl(a[i]);} pr(a.back());}
template<class T>void prarr(arr<T>& a){rep(i, (int)a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );}

// dijkstra法。ただし、全ての枝の距離が1のとき、BFS
void dijkstra(int st, arr<P>& G, vll& dist, vbool& seen, bool is_all_one=false){
    int V = G.size();
    priority_queue<P, vector<P>, greater<P>> min_heap;
    min_heap.push({0, st});
    dist.assign(V, INFL);
    seen.assign(V, false);
    dist[st] = 0;

    // 全ての枝の距離が1なら、単純なbfsで求められる。
    if(is_all_one){
        queue<int> que;
        que.push(st);
        seen[st] = true;
        while(!que.empty()){
            int now = que.front(); que.pop();
            ll d = dist[now];
            for(auto p: G[now]){
                int nx = p.second;
                if(seen[nx]) continue;
                seen[nx] = true;
                dist[nx] = d + 1;
                que.push(nx);
            }
        }
        return;
    }
 
    int frm;
    ll d;
    while(!min_heap.empty()){
        tie(d, frm) = min_heap.top(); min_heap.pop();
        if(seen[frm]) continue;
 
        // printf("node=%d", frm);
        seen[frm] = true;
        dist[frm] = d;
 
        for(P e: G[frm]){
            if(dist[e.second] > d+e.first)
                min_heap.push({d+e.first, e.second});
                // printf("(%d, %d) pushed", d+e.first, e.second);
        }
    }
}

int main()
{
    int n; cin >> n;
    if(n==1){
        pr(1);
        return 0;
    }

    arr<P> g(n+1);
    FOR(i, 1, n+1){
        int now = i;
        int k = popcount(i);

        if(i+k <= n) g[i].push_back({1, i+k});
        if(i-k > 0) g[i].push_back({1, i-k});
    }

    vll dist;
    vbool seen;
    dijkstra(1, g, dist, seen, 1);

    if(dist[n]==INFL){
        pr(-1);
    }else{
        pr(dist[n]+1);
    }

    // prvec(dist);
    return 0;}
0