結果

問題 No.3 ビットすごろく
ユーザー oxyshoweroxyshower
提出日時 2019-07-11 13:38:40
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 961 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 177,780 KB
実行使用メモリ 9,980 KB
最終ジャッジ日時 2024-07-01 09:25:01
合計ジャッジ時間 2,925 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif

struct edge{ int to,cost; };
const int N = 2e5+10;
vector< edge > G[N];

int n;
void dikstra(int s){
  typedef pair<int, int> P;
  const int INF = 1 << 30;

  priority_queue<P,vector<P>,greater<P>> que;
  vector<int> d(N,INF); //sからの最短距離
  d[s] = 0;
  que.push({0,s}); //{最短距離,頂点}

  while( !que.empty() ){
    P p = que.top(); que.pop();
    int v = p.second;
    if(d[v] < p.first) continue;
    for(auto e : G[v]){
      if(d[e.to] > d[v] + e.cost){
        d[e.to] = d[v] + e.cost;
        que.push( {d[e.to],e.to} );
      }
    }
  }
  if(d[n] == INF) cout << -1 << endl;
  else cout << d[n]+1 << endl;
}

signed main(){

  cin >> n;
  for(int i = 1; i <= n; i++){
    int cost = __builtin_popcount(i);
    G[i].push_back({i + cost,1});
    G[i].push_back({i - cost,1});
  }

  dikstra(1);

  return 0;
}
0