結果

問題 No.1283 Extra Fee
ユーザー tcltktcltk
提出日時 2021-01-21 07:45:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,653 ms / 2,000 ms
コード長 2,036 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,280 KB
実行使用メモリ 117,548 KB
最終ジャッジ日時 2024-04-27 23:46:40
合計ジャッジ時間 17,494 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
89,060 KB
testcase_01 AC 137 ms
89,032 KB
testcase_02 AC 138 ms
88,696 KB
testcase_03 AC 136 ms
88,660 KB
testcase_04 AC 136 ms
89,004 KB
testcase_05 AC 136 ms
89,052 KB
testcase_06 AC 139 ms
88,652 KB
testcase_07 AC 139 ms
88,836 KB
testcase_08 AC 139 ms
88,784 KB
testcase_09 AC 141 ms
89,180 KB
testcase_10 AC 139 ms
89,012 KB
testcase_11 AC 305 ms
91,936 KB
testcase_12 AC 276 ms
90,892 KB
testcase_13 AC 225 ms
90,064 KB
testcase_14 AC 325 ms
92,376 KB
testcase_15 AC 389 ms
93,564 KB
testcase_16 AC 241 ms
90,700 KB
testcase_17 AC 1,540 ms
115,920 KB
testcase_18 AC 794 ms
106,188 KB
testcase_19 AC 779 ms
104,044 KB
testcase_20 AC 763 ms
103,396 KB
testcase_21 AC 766 ms
103,684 KB
testcase_22 AC 678 ms
101,504 KB
testcase_23 AC 1,139 ms
99,452 KB
testcase_24 AC 1,159 ms
98,828 KB
testcase_25 AC 793 ms
104,128 KB
testcase_26 AC 801 ms
104,404 KB
testcase_27 AC 792 ms
103,956 KB
testcase_28 AC 805 ms
104,660 KB
testcase_29 AC 1,653 ms
117,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#region Header
#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
from queue import PriorityQueue
import bisect
import heapq

def input():
    return sys.stdin.readline()[:-1]

sys.setrecursionlimit(1000000)
#endregion

# _INPUT = """3 7
# 1 2 5
# 1 3 8
# 2 1 7
# 2 2 3
# 2 3 9
# 3 1 11
# 3 2 2
# """
# sys.stdin = io.StringIO(_INPUT)



def main():
    N, M = map(int, input().split())
    P = [[1 for _ in range(N)] for _ in range(N)]
    P[0][0] = 0

    for _ in range(M):
        h, w, c = map(int, input().split())
        h -= 1
        w -= 1
        P[h][w] = c + 1

    # dp[0] : 無料の権利を使っていない
    # dp[1] : 無料の権利を使った
    dp = [[[10**15 for _ in range(N)] for _ in range(N)] for _ in range(2)]

    hq = []
    heapq.heappush(hq, (0, (0, 0, 0)))
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    while hq:
        d, (flag, h, w) = heapq.heappop(hq)
        if d > dp[flag][h][w]:
            continue
        for (dh, dw) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            (h1, w1) = (h + dh, w + dw)
            if 0 <= h1 <= N-1 and 0 <= w1 <= N-1:
                if flag == 0:
                    d1 = dp[0][h][w] + P[h1][w1]
                    if d1 < dp[0][h1][w1]:
                        dp[0][h1][w1] = d1
                        heapq.heappush(hq, (d1, (0, h1, w1)))
                    if P[h1][w1] > 1:
                        d2 = dp[0][h][w] + 1
                        if d2 < dp[1][h1][w1]:
                            dp[1][h1][w1] = d2
                            heapq.heappush(hq, (d2, (1, h1, w1)))
                else:
                    d1 = dp[1][h][w] + P[h1][w1]
                    if d1 < dp[1][h1][w1]:
                        dp[1][h1][w1] = d1
                        heapq.heappush(hq, (d1, (1, h1, w1)))

    print(min(dp[0][N-1][N-1], dp[1][N-1][N-1]))


if __name__ == '__main__':
    main()
0