結果

問題 No.3 ビットすごろく
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 01:20:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 1,033 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 10,276 KB
最終ジャッジ日時 2023-09-14 01:39:10
合計ジャッジ時間 2,290 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,372 KB
testcase_01 AC 17 ms
8,268 KB
testcase_02 AC 18 ms
8,256 KB
testcase_03 AC 22 ms
8,744 KB
testcase_04 AC 18 ms
8,320 KB
testcase_05 AC 26 ms
9,284 KB
testcase_06 AC 22 ms
8,544 KB
testcase_07 AC 20 ms
8,568 KB
testcase_08 AC 25 ms
9,124 KB
testcase_09 AC 30 ms
9,676 KB
testcase_10 AC 33 ms
9,796 KB
testcase_11 AC 28 ms
9,452 KB
testcase_12 AC 26 ms
9,288 KB
testcase_13 AC 20 ms
8,564 KB
testcase_14 AC 33 ms
9,752 KB
testcase_15 AC 36 ms
10,276 KB
testcase_16 AC 34 ms
10,028 KB
testcase_17 AC 36 ms
10,192 KB
testcase_18 AC 21 ms
8,636 KB
testcase_19 AC 36 ms
10,136 KB
testcase_20 AC 18 ms
8,376 KB
testcase_21 AC 16 ms
8,256 KB
testcase_22 AC 32 ms
9,820 KB
testcase_23 AC 35 ms
10,160 KB
testcase_24 AC 36 ms
10,176 KB
testcase_25 AC 37 ms
10,224 KB
testcase_26 AC 16 ms
8,260 KB
testcase_27 AC 21 ms
8,672 KB
testcase_28 AC 35 ms
10,108 KB
testcase_29 AC 31 ms
9,480 KB
testcase_30 AC 17 ms
8,308 KB
testcase_31 AC 17 ms
8,312 KB
testcase_32 AC 28 ms
9,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import heapq
INF = 10**9

n = int(input())

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

class Dijkstra:
    def __init__(self, adj):
        self.adj = adj
        self.dist = [INF] * len(adj)
        self.q = []

    def calc(self, start):
        self.dist[start] = 0
        heapq.heappush(self.q, (0, start))
        while len(self.q) != 0:
            prov_cost, src = heapq.heappop(self.q)
            if self.dist[src] < prov_cost:
                continue
            for dest in self.adj[src]:
                if self.dist[dest] > self.dist[src] + 1:
                    self.dist[dest] = self.dist[src] + 1
                    heapq.heappush(self.q, (self.dist[dest], dest))
        return self.dist

DIJK = Dijkstra(edge)
DIJK.calc(0)
if DIJK.dist[-1] == INF:
    print(-1)
else:
    print(DIJK.dist[-1] + 1)
0