結果

問題 No.2095 High Rise
ユーザー lloyzlloyz
提出日時 2022-10-07 22:55:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,019 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 87,072 KB
最終ジャッジ日時 2023-09-03 14:44:47
合計ジャッジ時間 6,057 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,128 KB
testcase_01 AC 72 ms
71,400 KB
testcase_02 AC 71 ms
71,256 KB
testcase_03 AC 73 ms
71,512 KB
testcase_04 AC 74 ms
71,252 KB
testcase_05 AC 74 ms
71,440 KB
testcase_06 AC 74 ms
71,612 KB
testcase_07 AC 76 ms
71,252 KB
testcase_08 AC 74 ms
71,632 KB
testcase_09 AC 72 ms
71,300 KB
testcase_10 AC 73 ms
71,500 KB
testcase_11 AC 73 ms
71,292 KB
testcase_12 AC 87 ms
76,448 KB
testcase_13 AC 82 ms
76,716 KB
testcase_14 AC 86 ms
76,600 KB
testcase_15 AC 84 ms
76,608 KB
testcase_16 AC 82 ms
76,680 KB
testcase_17 AC 117 ms
77,836 KB
testcase_18 AC 110 ms
77,728 KB
testcase_19 AC 91 ms
76,688 KB
testcase_20 AC 107 ms
77,520 KB
testcase_21 AC 131 ms
81,244 KB
testcase_22 AC 250 ms
86,880 KB
testcase_23 AC 250 ms
86,900 KB
testcase_24 AC 249 ms
86,828 KB
testcase_25 AC 253 ms
86,912 KB
testcase_26 AC 248 ms
87,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
if n == 1:
    print(0)
    exit()

INF = 10**18
DP = [[INF for _ in range(m)] for _ in range(n - 1)]
prev_min_c = INF
prev_min_c_pos = set()
for i in range(m):
    DP[0][i] = A[0][i] + A[1][i]
    if DP[0][i] < prev_min_c:
        prev_min_c = DP[0][i]
        prev_min_c_pos = set([i])
    elif DP[0][i] == prev_min_c:
        prev_min_c_pos.add(i)
for i in range(2, n):
    curr_min_c = INF
    curr_min_c_pos = set()
    for j in range(m):
        if j in prev_min_c_pos:
            DP[i - 1][j] = min(DP[i - 2][j] + A[i][j], prev_min_c + A[i][j])
        else:
            DP[i - 1][j] = min(DP[i - 2][j] + A[i][j], prev_min_c + A[i][j] + A[i - 1][j])
        if DP[i - 1][j] < curr_min_c:
            curr_min_c = DP[i - 1][j]
            curr_min_c_pos = set([j])
        elif DP[i - 1][j] == curr_min_c:
            curr_min_c_pos.add(j)
    prev_min_c = curr_min_c
    prev_min_c_pos = curr_min_c_pos
print(min(DP[-1]))
0