結果

問題 No.3 ビットすごろく
ユーザー tanon710tanon710
提出日時 2020-08-20 22:23:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 1,091 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 79,892 KB
最終ジャッジ日時 2023-09-14 01:49:14
合計ジャッジ時間 5,869 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,380 KB
testcase_01 AC 78 ms
71,220 KB
testcase_02 AC 74 ms
71,072 KB
testcase_03 AC 115 ms
78,468 KB
testcase_04 AC 90 ms
76,520 KB
testcase_05 AC 136 ms
79,528 KB
testcase_06 AC 122 ms
78,068 KB
testcase_07 AC 108 ms
77,824 KB
testcase_08 AC 133 ms
78,888 KB
testcase_09 AC 145 ms
79,336 KB
testcase_10 AC 147 ms
79,464 KB
testcase_11 AC 138 ms
79,348 KB
testcase_12 AC 135 ms
79,520 KB
testcase_13 AC 117 ms
78,240 KB
testcase_14 AC 145 ms
79,404 KB
testcase_15 AC 155 ms
79,848 KB
testcase_16 AC 151 ms
79,872 KB
testcase_17 AC 155 ms
79,892 KB
testcase_18 AC 115 ms
78,344 KB
testcase_19 AC 161 ms
79,712 KB
testcase_20 AC 83 ms
75,632 KB
testcase_21 AC 73 ms
71,016 KB
testcase_22 AC 145 ms
79,168 KB
testcase_23 AC 154 ms
79,588 KB
testcase_24 AC 156 ms
79,840 KB
testcase_25 AC 159 ms
79,816 KB
testcase_26 AC 76 ms
71,560 KB
testcase_27 AC 118 ms
78,164 KB
testcase_28 AC 149 ms
79,760 KB
testcase_29 AC 140 ms
79,664 KB
testcase_30 AC 77 ms
75,472 KB
testcase_31 AC 79 ms
75,528 KB
testcase_32 AC 138 ms
79,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

#edgeには[weight,v]を追加していく
def dijkstra(s,edge):
    #始点sから各頂点への最短距離
    d = [float("inf")] * (n+1)
    used = [True] * (n+1) #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d

def popcount(n):
    ret=0
    for i in range(20):
        if n&(1<<i):
            ret+=1
    return ret

n=int(input())
edges=[[] for _ in range(n+1)]
for v in range(1,n+1):
    pc=popcount(v)
    if v-pc>=1:
        edges[v].append((1,v-pc))
    if v+pc<=n:
        edges[v].append((1,v+pc))
ans=dijkstra(1,edges)
if ans[n]==float('inf'):
    print(-1)
else:
    print(ans[n]+1)
0