結果

問題 No.2673 A present from B
ユーザー miya145592miya145592
提出日時 2024-03-16 02:16:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 681 ms / 2,000 ms
コード長 946 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 243,828 KB
最終ジャッジ日時 2024-03-16 02:16:43
合計ジャッジ時間 7,123 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,600 KB
testcase_01 AC 44 ms
55,600 KB
testcase_02 AC 40 ms
55,600 KB
testcase_03 AC 40 ms
55,600 KB
testcase_04 AC 59 ms
68,492 KB
testcase_05 AC 41 ms
55,600 KB
testcase_06 AC 667 ms
243,828 KB
testcase_07 AC 681 ms
243,588 KB
testcase_08 AC 658 ms
243,584 KB
testcase_09 AC 56 ms
66,388 KB
testcase_10 AC 60 ms
68,488 KB
testcase_11 AC 54 ms
66,400 KB
testcase_12 AC 224 ms
123,388 KB
testcase_13 AC 226 ms
122,864 KB
testcase_14 AC 333 ms
154,308 KB
testcase_15 AC 162 ms
102,548 KB
testcase_16 AC 181 ms
101,632 KB
testcase_17 AC 125 ms
90,520 KB
testcase_18 AC 172 ms
105,304 KB
testcase_19 AC 64 ms
70,708 KB
testcase_20 AC 279 ms
139,644 KB
testcase_21 AC 544 ms
206,180 KB
testcase_22 AC 42 ms
55,600 KB
testcase_23 AC 41 ms
55,600 KB
testcase_24 AC 41 ms
55,600 KB
testcase_25 AC 40 ms
55,600 KB
testcase_26 AC 41 ms
55,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = list(map(int, input().split()))
L = (2*M+3)*N
G = [set() for _ in range(L)]
for i in range(N):
    for j in range(L//N-1):
        G[(j+1)*N+i].add((j*N+i, 0))
for i in range(1, 2*M+3, 2):
    for j in range(N-1):
        G[i*N+j].add((i*N+j+1, 1))
        G[i*N+j+1].add((i*N+j, 1))
for i, a in enumerate(A):
    a-=1
    u = (i*2+2)*N+a
    v = (i*2+2)*N+a+1
    G[u].add((v-N, 0))
    G[v].add((u-N, 0))
    G[u].discard((u-N, 0))
    G[v].discard((v-N, 0))
#print(G)
deq = deque()
st = (2*M+2)*N
deq.append(st)
INF = 10**9
dist = [INF for _ in range(L)]
dist[st] = 0
while deq:
    v = deq.popleft()
    for nv, cost in G[v]:
        if dist[nv]>dist[v]+cost:
            dist[nv] = dist[v] + cost
            if cost==0:
                deq.appendleft(nv)
            else:
                deq.append(nv)
#print(dist)
print(*dist[1:N])
0