結果

問題 No.3 ビットすごろく
ユーザー caleicalei
提出日時 2019-12-28 19:42:34
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 673 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-07-01 09:36:24
合計ジャッジ時間 2,555 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

# 2019/12/28

from heapq import heappop,heappush

n=int(input())

edge=[[] for _ in range(n+1)]
for i in range(n+1):
    u,v=i,i+bin(i).count('1')
    if v<=n:
        edge[u].append((u,v))
    u,v=i,i-bin(i).count('1')
    if v>0:
        edge[u].append((u,v))


def dijkstra(p,s):
    d=[float('inf')]*(n+1)
    d[s]=1
    que=[]
    for e in edge[s]:
        heappush(que,e)
    while que:
        p,v=heappop(que)
        if d[v]<=d[p]+1:continue
        d[v]=d[p]+1
        for pp,vtx in edge[v]:
            if d[vtx]<=d[pp]+1:continue
            hq=(pp,vtx)
            heappush(que,hq)
    return d


res=dijkstra(0,1)
print(res[n] if res[n]!=float('inf') else -1)
0