結果

問題 No.3 ビットすごろく
ユーザー calei
提出日時 2019-12-28 19:38:47
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 741 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-10-12 10:17:31
合計ジャッジ時間 2,547 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

# 2019/12/28

from heapq import heappop,heappush

n=int(input())

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


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


res=dijkstra(1)
print(res[n])
0