結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
54,348 KB
testcase_01 AC 33 ms
52,988 KB
testcase_02 AC 36 ms
52,952 KB
testcase_03 AC 37 ms
53,104 KB
testcase_04 AC 36 ms
54,528 KB
testcase_05 AC 35 ms
53,124 KB
testcase_06 AC 37 ms
53,932 KB
testcase_07 AC 35 ms
53,292 KB
testcase_08 AC 41 ms
59,312 KB
testcase_09 AC 36 ms
53,560 KB
testcase_10 AC 36 ms
54,372 KB
testcase_11 AC 184 ms
90,040 KB
testcase_12 AC 187 ms
93,984 KB
testcase_13 AC 131 ms
88,488 KB
testcase_14 AC 289 ms
130,620 KB
testcase_15 AC 410 ms
162,568 KB
testcase_16 AC 164 ms
95,336 KB
testcase_17 AC 1,511 ms
303,984 KB
testcase_18 AC 1,503 ms
355,228 KB
testcase_19 AC 1,568 ms
374,636 KB
testcase_20 AC 1,507 ms
354,824 KB
testcase_21 AC 1,544 ms
359,692 KB
testcase_22 AC 1,233 ms
329,776 KB
testcase_23 AC 1,566 ms
383,344 KB
testcase_24 AC 1,601 ms
383,000 KB
testcase_25 AC 1,575 ms
384,184 KB
testcase_26 AC 1,571 ms
384,096 KB
testcase_27 AC 1,650 ms
384,564 KB
testcase_28 AC 1,583 ms
384,152 KB
testcase_29 AC 1,411 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