結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2020-05-20 01:50:24 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 62 ms / 5,000 ms |
コード長 | 884 bytes |
コンパイル時間 | 90 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 14,208 KB |
最終ジャッジ日時 | 2024-07-01 09:47:41 |
合計ジャッジ時間 | 2,539 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
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)