結果

問題 No.1283 Extra Fee
ユーザー brthyyjpbrthyyjp
提出日時 2020-11-06 22:45:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,357 ms / 2,000 ms
コード長 1,440 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 227,236 KB
最終ジャッジ日時 2024-04-27 23:24:01
合計ジャッジ時間 17,719 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,808 KB
testcase_01 AC 36 ms
53,956 KB
testcase_02 AC 39 ms
53,800 KB
testcase_03 AC 36 ms
53,288 KB
testcase_04 AC 34 ms
54,988 KB
testcase_05 AC 36 ms
53,796 KB
testcase_06 AC 42 ms
59,692 KB
testcase_07 AC 35 ms
53,872 KB
testcase_08 AC 40 ms
60,572 KB
testcase_09 AC 39 ms
54,312 KB
testcase_10 AC 37 ms
55,028 KB
testcase_11 AC 281 ms
85,420 KB
testcase_12 AC 206 ms
85,972 KB
testcase_13 AC 189 ms
83,804 KB
testcase_14 AC 316 ms
103,480 KB
testcase_15 AC 393 ms
118,952 KB
testcase_16 AC 201 ms
86,828 KB
testcase_17 AC 1,274 ms
210,072 KB
testcase_18 AC 1,004 ms
212,828 KB
testcase_19 AC 1,053 ms
222,684 KB
testcase_20 AC 1,003 ms
211,588 KB
testcase_21 AC 970 ms
213,604 KB
testcase_22 AC 947 ms
200,760 KB
testcase_23 AC 1,008 ms
218,004 KB
testcase_24 AC 1,014 ms
221,408 KB
testcase_25 AC 1,077 ms
227,236 KB
testcase_26 AC 1,115 ms
227,208 KB
testcase_27 AC 1,078 ms
226,780 KB
testcase_28 AC 1,076 ms
227,224 KB
testcase_29 AC 1,357 ms
220,384 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