結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:15:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,774 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,020 KB
実行使用メモリ 400,400 KB
最終ジャッジ日時 2024-07-22 14:06:52
合計ジャッジ時間 24,996 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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