結果

問題 No.1283 Extra Fee
ユーザー ayaoniayaoni
提出日時 2020-11-07 00:43:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,838 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 329,116 KB
最終ジャッジ日時 2023-09-29 19:53:10
合計ジャッジ時間 10,698 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,620 KB
testcase_01 AC 68 ms
71,500 KB
testcase_02 AC 68 ms
71,260 KB
testcase_03 AC 70 ms
71,380 KB
testcase_04 AC 71 ms
71,248 KB
testcase_05 AC 72 ms
71,380 KB
testcase_06 AC 73 ms
71,232 KB
testcase_07 AC 69 ms
71,196 KB
testcase_08 AC 71 ms
71,124 KB
testcase_09 AC 74 ms
71,216 KB
testcase_10 AC 70 ms
71,128 KB
testcase_11 AC 301 ms
88,764 KB
testcase_12 AC 328 ms
93,360 KB
testcase_13 AC 303 ms
92,004 KB
testcase_14 AC 700 ms
137,208 KB
testcase_15 AC 980 ms
173,532 KB
testcase_16 AC 367 ms
99,064 KB
testcase_17 AC 1,707 ms
238,352 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N,M = MI()
HWC = [tuple(MI()) for _ in range(M)]

Graph = [[] for _ in range(N**2)]
for i in range(N):
    for j in range(N-1):
        a,b = N*i+j,N*i+j+1
        Graph[a].append([b,1])
        Graph[b].append([a,1])
for i in range(N-1):
    for j in range(N):
        a,b = N*i+j,N*(i+1)+j
        Graph[a].append([b,1])
        Graph[b].append([a,1])

for h,w,c in HWC:
    h -= 1
    w -= 1
    b = N*h+w
    if h >= 1:
        a = N*(h-1)+w
        Graph[a] = [x for x in Graph[a] if x[0] != b]+[[b,c+1]]
    if h <= N-2:
        a = N*(h+1)+w
        Graph[a] = [x for x in Graph[a] if x[0] != b]+[[b,c+1]]
    if w >= 1:
        a = N*h+w-1
        Graph[a] = [x for x in Graph[a] if x[0] != b]+[[b,c+1]]
    if w <= N-2:
        a = N*h+w+1
        Graph[a] = [x for x in Graph[a] if x[0] != b]+[[b,c+1]]

inf = 10**18

dist0 = [inf]*(N**2)  # (1,1)からの最短距離
dist0[0] = 0
q0 = []
heapq.heappush(q0,(0,0))
v0 = [0]*(N**2)
while q0:
    k,u = heapq.heappop(q0)
    if v0[u]:
        continue
    v0[u] = True
    for x in Graph[u]:
        uv,ud = x
        if v0[uv]:
            continue
        vd = k + ud
        if dist0[uv] > vd:
            dist0[uv] = vd
            heapq.heappush(q0,(vd,uv))

dist1 = [inf]*(N**2)  # (N,N)からの最短距離
dist1[N**2-1] = 0
q1 = []
heapq.heappush(q1,(0,N**2-1))
v1 = [0]*(N**2)
while q1:
    k,u = heapq.heappop(q1)
    if v1[u]:
        continue
    v1[u] = True
    for x in Graph[u]:
        uv,ud = x
        if v1[uv]:
            continue
        vd = k + ud
        if dist1[uv] > vd:
            dist1[uv] = vd
            heapq.heappush(q1,(vd,uv))

ans = dist0[-1]
for h,w,c in HWC:
    h -= 1
    w -= 1
    a = N*h+w
    ans = min(ans,dist0[a]+dist1[a]-2*c)
print(ans)
0