結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:15:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,920 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 401,524 KB
最終ジャッジ日時 2023-09-29 20:02:22
合計ジャッジ時間 27,471 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,404 KB
testcase_01 AC 75 ms
71,232 KB
testcase_02 AC 76 ms
71,232 KB
testcase_03 AC 74 ms
71,372 KB
testcase_04 AC 77 ms
71,336 KB
testcase_05 AC 75 ms
71,160 KB
testcase_06 AC 78 ms
71,156 KB
testcase_07 AC 76 ms
71,516 KB
testcase_08 AC 83 ms
75,548 KB
testcase_09 AC 77 ms
71,160 KB
testcase_10 AC 77 ms
71,228 KB
testcase_11 AC 257 ms
92,092 KB
testcase_12 AC 232 ms
96,004 KB
testcase_13 AC 191 ms
91,020 KB
testcase_14 AC 370 ms
132,552 KB
testcase_15 AC 480 ms
164,332 KB
testcase_16 AC 213 ms
96,792 KB
testcase_17 AC 1,445 ms
309,004 KB
testcase_18 AC 1,674 ms
364,416 KB
testcase_19 AC 1,769 ms
390,936 KB
testcase_20 AC 1,718 ms
363,612 KB
testcase_21 AC 1,828 ms
373,256 KB
testcase_22 AC 1,472 ms
337,912 KB
testcase_23 AC 1,727 ms
388,512 KB
testcase_24 AC 1,812 ms
399,840 KB
testcase_25 AC 1,876 ms
401,060 KB
testcase_26 AC 1,920 ms
401,448 KB
testcase_27 AC 1,907 ms
401,324 KB
testcase_28 AC 1,844 ms
401,524 KB
testcase_29 AC 1,618 ms
326,552 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=[[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