結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:43:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,769 ms / 2,000 ms
コード長 1,138 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 257,976 KB
最終ジャッジ日時 2024-04-27 23:33:20
合計ジャッジ時間 23,020 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,024 KB
testcase_01 AC 42 ms
53,972 KB
testcase_02 AC 41 ms
53,616 KB
testcase_03 AC 41 ms
54,776 KB
testcase_04 AC 40 ms
53,564 KB
testcase_05 AC 41 ms
52,928 KB
testcase_06 AC 44 ms
54,400 KB
testcase_07 AC 40 ms
53,932 KB
testcase_08 AC 46 ms
59,524 KB
testcase_09 AC 41 ms
54,092 KB
testcase_10 AC 41 ms
53,668 KB
testcase_11 AC 207 ms
84,512 KB
testcase_12 AC 188 ms
85,756 KB
testcase_13 AC 155 ms
83,688 KB
testcase_14 AC 333 ms
107,248 KB
testcase_15 AC 464 ms
123,804 KB
testcase_16 AC 174 ms
87,276 KB
testcase_17 AC 1,111 ms
195,236 KB
testcase_18 AC 1,467 ms
243,328 KB
testcase_19 AC 1,573 ms
253,140 KB
testcase_20 AC 1,451 ms
241,312 KB
testcase_21 AC 1,540 ms
243,704 KB
testcase_22 AC 1,325 ms
223,628 KB
testcase_23 AC 1,365 ms
249,280 KB
testcase_24 AC 1,494 ms
252,888 KB
testcase_25 AC 1,616 ms
257,928 KB
testcase_26 AC 1,769 ms
257,976 KB
testcase_27 AC 1,670 ms
257,768 KB
testcase_28 AC 1,621 ms
257,828 KB
testcase_29 AC 1,195 ms
204,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input=sys.stdin.readline

def dijkstra(s,e):
    hq = [(0, s)]
    heapq.heapify(hq)
    cost = [float('inf')] * len(e)
    cost[s] = 0
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]:
            continue
        for u, d in e[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost

n,m=map(int,input().split())
fees={}
for _ in range(m):
    y,x,cost=map(int,input().split())
    fees[(x-1,y-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
                if (x+dx,y+dy) in fees:
                    g[u].append((v,1+fees[(x+dx,y+dy)]))
                    g[u].append((v+n*n,1))
                    g[u+n*n].append((v+n*n,1+fees[(x+dx,y+dy)]))
                else:
                    g[u].append((v,1))
                    g[u+n*n].append((v+n*n,1))
ans=dijkstra(0,g)
print(min(ans[n*n-1],ans[2*n*n-1]))
0