結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:32:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,607 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 384,500 KB
最終ジャッジ日時 2024-04-27 23:31:48
合計ジャッジ時間 22,557 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
53,132 KB
testcase_01 AC 32 ms
53,996 KB
testcase_02 AC 33 ms
54,056 KB
testcase_03 AC 36 ms
53,016 KB
testcase_04 AC 34 ms
52,620 KB
testcase_05 AC 32 ms
53,316 KB
testcase_06 AC 35 ms
53,540 KB
testcase_07 AC 34 ms
53,288 KB
testcase_08 AC 39 ms
59,664 KB
testcase_09 AC 35 ms
53,728 KB
testcase_10 AC 35 ms
55,320 KB
testcase_11 AC 172 ms
90,312 KB
testcase_12 AC 171 ms
94,060 KB
testcase_13 AC 131 ms
88,600 KB
testcase_14 AC 283 ms
130,776 KB
testcase_15 AC 413 ms
162,512 KB
testcase_16 AC 161 ms
95,248 KB
testcase_17 AC 1,311 ms
304,244 KB
testcase_18 AC 1,451 ms
355,712 KB
testcase_19 AC 1,532 ms
374,656 KB
testcase_20 AC 1,444 ms
354,680 KB
testcase_21 AC 1,565 ms
359,696 KB
testcase_22 AC 1,209 ms
329,476 KB
testcase_23 AC 1,582 ms
382,856 KB
testcase_24 AC 1,538 ms
383,040 KB
testcase_25 AC 1,584 ms
384,020 KB
testcase_26 AC 1,566 ms
384,412 KB
testcase_27 AC 1,607 ms
384,500 KB
testcase_28 AC 1,586 ms
384,488 KB
testcase_29 AC 1,392 ms
324,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input=sys.stdin.readline

def calc():
    n,m=map(int,input().split())
    fees=[1]*(n*n)
    for _ in range(m):
        y,x,cost=map(int,input().split())
        fees[n*(y-1)+x-1]+=cost
    g=[[] for _ in range(2*n*n)]
    for y in range(n):
        for x in range(n):
            for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)]:
                if 0<=x+dx<=n-1 and 0<=y+dy<=n-1:
                    u=n*y+x
                    v=n*(y+dy)+x+dx
                    fee=fees[n*(y+dy)+x+dx]
                    if fee!=1:
                        g[u].append([v,fee])
                        g[u].append([v+n*n,1])
                        g[u+n*n].append([v+n*n,fee])
                    else:
                        g[u].append([v,1])
                        g[u+n*n].append([v+n*n,1])
    hq = [(0, 0)]
    heapq.heapify(hq)
    INF=10**15
    cost = [INF]*(2*n*n)
    cost[0] = 0
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]:
            continue
        for u, d in g[v]:
            tmp=d+cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    print(min(cost[n*n-1],cost[2*n*n-1]))
calc()
0