結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:06:06
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 80,248 KB
最終ジャッジ日時 2023-09-29 20:01:00
合計ジャッジ時間 6,802 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input=sys.stdin.readilne

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**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 (x+dx,y+dy) in fees:
                    g[u].append([v,1+fees[(x+dx,y+dy)]])
                    g[u].append([v+n**2,1])
                    g[u+n**2].append([v+n**2,1+fees[(x+dx,y+dy)]])
                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