結果

問題 No.1283 Extra Fee
ユーザー brthyyjpbrthyyjp
提出日時 2020-11-06 22:45:34
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,621 ms / 2,000 ms
コード長 1,440 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 229,516 KB
最終ジャッジ日時 2023-08-10 06:06:03
合計ジャッジ時間 21,684 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,676 KB
testcase_01 AC 73 ms
71,212 KB
testcase_02 AC 74 ms
71,512 KB
testcase_03 AC 73 ms
71,400 KB
testcase_04 AC 71 ms
71,400 KB
testcase_05 AC 72 ms
71,588 KB
testcase_06 AC 78 ms
75,744 KB
testcase_07 AC 75 ms
71,512 KB
testcase_08 AC 79 ms
75,808 KB
testcase_09 AC 75 ms
71,344 KB
testcase_10 AC 74 ms
71,516 KB
testcase_11 AC 356 ms
86,956 KB
testcase_12 AC 278 ms
87,816 KB
testcase_13 AC 252 ms
84,936 KB
testcase_14 AC 400 ms
105,800 KB
testcase_15 AC 479 ms
120,272 KB
testcase_16 AC 271 ms
88,052 KB
testcase_17 AC 1,519 ms
212,480 KB
testcase_18 AC 1,195 ms
214,764 KB
testcase_19 AC 1,260 ms
224,052 KB
testcase_20 AC 1,196 ms
213,688 KB
testcase_21 AC 1,156 ms
215,180 KB
testcase_22 AC 1,133 ms
202,588 KB
testcase_23 AC 1,206 ms
220,828 KB
testcase_24 AC 1,206 ms
223,032 KB
testcase_25 AC 1,286 ms
229,252 KB
testcase_26 AC 1,306 ms
229,516 KB
testcase_27 AC 1,271 ms
228,764 KB
testcase_28 AC 1,306 ms
228,824 KB
testcase_29 AC 1,621 ms
222,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, m = map(int, input().split())
HWC = []
A = [[0]*n for _ in range(n)]
for i in range(m):
    h, w, c = map(int, input().split())
    h, w =h-1, w-1
    HWC.append((h, w, c))
    A[h][w] = c

import heapq
INF = 10**18
def dijkstra_heap(s, edge):
    n = len(edge)
    d = [INF] * n
    used = [True] * n #True: not used
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v],e[1]))
    return d

ans = INF

g = [[] for _ in range(n**2)]
rg = [[] for _ in range(n**2)]
for i in range(n):
    for j in range(n):
        v = i*n+j
        for di, dj in (-1, 0), (1, 0), (0, 1), (0, -1):
            ni, nj = i+di, j+dj
            if 0 <= ni < n and 0 <= nj < n:
                c = A[ni][nj]+1
                u = ni*n+nj
                g[v].append((c, u))
                rg[u].append((c, v))
d1 = dijkstra_heap(0, g)
d2 = dijkstra_heap(n**2-1, rg)
#print(d1)
#print(d2)
ans = 10**18
for v in range(1, n**2-1):
    i, j = divmod(v, n)
    temp = d1[v]+d2[v]-A[i][j]
    ans = min(ans, temp)
print(ans)
0