結果

問題 No.1283 Extra Fee
ユーザー tanon710tanon710
提出日時 2020-11-07 02:04:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,110 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 414,480 KB
最終ジャッジ日時 2023-09-29 20:00:53
合計ジャッジ時間 34,920 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,360 KB
testcase_01 AC 74 ms
71,360 KB
testcase_02 AC 75 ms
71,388 KB
testcase_03 AC 73 ms
71,264 KB
testcase_04 AC 74 ms
71,404 KB
testcase_05 AC 75 ms
71,344 KB
testcase_06 AC 77 ms
71,148 KB
testcase_07 AC 75 ms
71,504 KB
testcase_08 AC 80 ms
75,572 KB
testcase_09 AC 76 ms
71,420 KB
testcase_10 AC 76 ms
71,420 KB
testcase_11 AC 277 ms
92,876 KB
testcase_12 AC 263 ms
96,700 KB
testcase_13 AC 218 ms
91,744 KB
testcase_14 AC 446 ms
137,520 KB
testcase_15 AC 667 ms
171,556 KB
testcase_16 AC 246 ms
98,020 KB
testcase_17 AC 1,628 ms
312,076 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 1,898 ms
354,768 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 AC 1,689 ms
332,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

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