結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:26:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,723 ms / 2,000 ms
コード長 1,040 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 384,580 KB
最終ジャッジ日時 2024-04-27 23:30:54
合計ジャッジ時間 24,405 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,488 KB
testcase_01 AC 33 ms
53,352 KB
testcase_02 AC 33 ms
52,892 KB
testcase_03 AC 33 ms
53,272 KB
testcase_04 AC 38 ms
53,568 KB
testcase_05 AC 35 ms
54,008 KB
testcase_06 AC 39 ms
55,904 KB
testcase_07 AC 35 ms
53,376 KB
testcase_08 AC 41 ms
60,252 KB
testcase_09 AC 36 ms
54,656 KB
testcase_10 AC 35 ms
54,244 KB
testcase_11 AC 180 ms
90,420 KB
testcase_12 AC 176 ms
93,852 KB
testcase_13 AC 140 ms
88,868 KB
testcase_14 AC 297 ms
130,972 KB
testcase_15 AC 424 ms
162,708 KB
testcase_16 AC 168 ms
95,248 KB
testcase_17 AC 1,321 ms
304,056 KB
testcase_18 AC 1,507 ms
355,452 KB
testcase_19 AC 1,628 ms
374,520 KB
testcase_20 AC 1,669 ms
354,844 KB
testcase_21 AC 1,622 ms
359,200 KB
testcase_22 AC 1,262 ms
330,068 KB
testcase_23 AC 1,661 ms
383,192 KB
testcase_24 AC 1,632 ms
383,416 KB
testcase_25 AC 1,624 ms
384,232 KB
testcase_26 AC 1,720 ms
384,280 KB
testcase_27 AC 1,723 ms
384,312 KB
testcase_28 AC 1,635 ms
384,580 KB
testcase_29 AC 1,446 ms
324,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input=sys.stdin.readline

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**18
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]))
0