結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:30:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,760 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 385,964 KB
最終ジャッジ日時 2023-09-29 20:04:23
合計ジャッジ時間 26,476 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,288 KB
testcase_01 AC 77 ms
71,416 KB
testcase_02 AC 77 ms
71,500 KB
testcase_03 AC 71 ms
71,280 KB
testcase_04 AC 77 ms
71,460 KB
testcase_05 AC 72 ms
71,408 KB
testcase_06 AC 77 ms
71,552 KB
testcase_07 AC 78 ms
71,548 KB
testcase_08 AC 79 ms
75,828 KB
testcase_09 AC 75 ms
71,336 KB
testcase_10 AC 75 ms
71,376 KB
testcase_11 AC 237 ms
92,340 KB
testcase_12 AC 224 ms
95,952 KB
testcase_13 AC 182 ms
90,580 KB
testcase_14 AC 348 ms
132,216 KB
testcase_15 AC 467 ms
164,016 KB
testcase_16 AC 208 ms
97,204 KB
testcase_17 AC 1,428 ms
305,912 KB
testcase_18 AC 1,615 ms
356,820 KB
testcase_19 AC 1,710 ms
376,100 KB
testcase_20 AC 1,551 ms
356,304 KB
testcase_21 AC 1,692 ms
361,752 KB
testcase_22 AC 1,377 ms
331,000 KB
testcase_23 AC 1,733 ms
384,584 KB
testcase_24 AC 1,683 ms
384,488 KB
testcase_25 AC 1,716 ms
385,964 KB
testcase_26 AC 1,668 ms
385,708 KB
testcase_27 AC 1,760 ms
385,904 KB
testcase_28 AC 1,686 ms
385,532 KB
testcase_29 AC 1,507 ms
326,868 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**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]))
0