結果

問題 No.3 ビットすごろく
ユーザー Yuta123456Yuta123456
提出日時 2020-05-20 01:50:24
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 884 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 11,848 KB
最終ジャッジ日時 2023-09-14 01:43:37
合計ジャッジ時間 1,889 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
8,268 KB
testcase_01 AC 14 ms
8,220 KB
testcase_02 AC 14 ms
8,212 KB
testcase_03 AC 20 ms
9,108 KB
testcase_04 AC 15 ms
8,516 KB
testcase_05 AC 24 ms
10,136 KB
testcase_06 AC 19 ms
9,140 KB
testcase_07 AC 16 ms
8,748 KB
testcase_08 AC 22 ms
9,816 KB
testcase_09 AC 28 ms
10,460 KB
testcase_10 AC 30 ms
11,116 KB
testcase_11 AC 26 ms
10,468 KB
testcase_12 AC 24 ms
10,140 KB
testcase_13 AC 17 ms
8,984 KB
testcase_14 AC 30 ms
11,160 KB
testcase_15 AC 34 ms
11,632 KB
testcase_16 AC 32 ms
11,284 KB
testcase_17 AC 33 ms
11,696 KB
testcase_18 AC 19 ms
8,816 KB
testcase_19 AC 35 ms
11,688 KB
testcase_20 AC 14 ms
8,448 KB
testcase_21 AC 13 ms
8,232 KB
testcase_22 AC 30 ms
10,976 KB
testcase_23 AC 35 ms
11,764 KB
testcase_24 AC 34 ms
11,848 KB
testcase_25 AC 34 ms
11,672 KB
testcase_26 AC 13 ms
8,348 KB
testcase_27 AC 18 ms
9,044 KB
testcase_28 AC 32 ms
11,208 KB
testcase_29 AC 26 ms
10,468 KB
testcase_30 AC 13 ms
8,320 KB
testcase_31 AC 14 ms
8,380 KB
testcase_32 AC 26 ms
10,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
adjacent_list = [[] for i in range(n+1)]
for i in range(1,n+1):
    count = "{:b}".format(i).count('1')
    if i - count >= 1:
        adjacent_list[i].append([i-count,1])
    if i + count <= n:
        adjacent_list[i].append([i+count,1])
from heapq import heappush, heappop
def dijkstra(start,graph):
    INF = 10 ** 15
    dist = [INF] * (n+1)
    dist[start] = 0
    q = [(0,start)]
    while q:
        d,v = heappop(q)
        if dist[v] < d:
            continue
        for w,a in graph[v]:
            d1 = d + a
            if dist[w] > d1:
                dist[w] = d1
                heappush(q, (d1,w))
    return dist
#このまま適当にはって使える感じではない?
#隣接リスト適当に渡せば動く。重みを追加するのをわすれずに
d = dijkstra(1,adjacent_list)
if d[-1] == 10**15:
    print(-1)
else:
    print(d[-1]+1)
0