結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:39:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 894 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 217,220 KB
最終ジャッジ日時 2024-04-27 23:32:44
合計ジャッジ時間 13,194 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
54,148 KB
testcase_01 AC 36 ms
53,128 KB
testcase_02 AC 32 ms
53,112 KB
testcase_03 AC 35 ms
53,840 KB
testcase_04 AC 33 ms
53,688 KB
testcase_05 AC 32 ms
53,512 KB
testcase_06 AC 34 ms
53,976 KB
testcase_07 AC 33 ms
54,732 KB
testcase_08 AC 36 ms
58,972 KB
testcase_09 AC 33 ms
54,092 KB
testcase_10 AC 34 ms
53,320 KB
testcase_11 AC 159 ms
83,496 KB
testcase_12 AC 144 ms
84,892 KB
testcase_13 AC 113 ms
82,684 KB
testcase_14 AC 218 ms
101,620 KB
testcase_15 AC 287 ms
116,384 KB
testcase_16 AC 133 ms
84,832 KB
testcase_17 AC 824 ms
192,680 KB
testcase_18 AC 743 ms
206,480 KB
testcase_19 AC 814 ms
212,736 KB
testcase_20 AC 733 ms
204,788 KB
testcase_21 AC 804 ms
207,260 KB
testcase_22 AC 696 ms
192,156 KB
testcase_23 AC 766 ms
217,220 KB
testcase_24 AC 761 ms
215,868 KB
testcase_25 AC 800 ms
216,524 KB
testcase_26 AC 765 ms
216,292 KB
testcase_27 AC 808 ms
216,696 KB
testcase_28 AC 813 ms
216,756 KB
testcase_29 AC 894 ms
202,764 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