結果

問題 No.2328 Build Walls
ユーザー cozy_saunacozy_sauna
提出日時 2023-05-28 15:16:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,817 ms / 3,000 ms
コード長 3,206 bytes
コンパイル時間 1,349 ms
コンパイル使用メモリ 86,528 KB
実行使用メモリ 319,448 KB
最終ジャッジ日時 2023-08-27 11:48:07
合計ジャッジ時間 25,445 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 256 ms
90,292 KB
testcase_01 AC 253 ms
90,172 KB
testcase_02 AC 250 ms
90,144 KB
testcase_03 AC 257 ms
90,308 KB
testcase_04 AC 259 ms
90,312 KB
testcase_05 AC 257 ms
90,248 KB
testcase_06 AC 256 ms
90,232 KB
testcase_07 AC 253 ms
90,328 KB
testcase_08 AC 258 ms
90,312 KB
testcase_09 AC 256 ms
90,300 KB
testcase_10 AC 249 ms
90,092 KB
testcase_11 AC 251 ms
90,200 KB
testcase_12 AC 247 ms
90,304 KB
testcase_13 AC 506 ms
135,140 KB
testcase_14 AC 687 ms
146,200 KB
testcase_15 AC 584 ms
126,688 KB
testcase_16 AC 390 ms
100,648 KB
testcase_17 AC 579 ms
125,024 KB
testcase_18 AC 329 ms
95,400 KB
testcase_19 AC 305 ms
97,100 KB
testcase_20 AC 355 ms
98,116 KB
testcase_21 AC 362 ms
115,844 KB
testcase_22 AC 884 ms
175,972 KB
testcase_23 AC 1,304 ms
217,932 KB
testcase_24 AC 1,144 ms
205,948 KB
testcase_25 AC 1,212 ms
215,188 KB
testcase_26 AC 968 ms
184,068 KB
testcase_27 AC 1,121 ms
199,396 KB
testcase_28 AC 389 ms
133,324 KB
testcase_29 AC 1,224 ms
217,192 KB
testcase_30 AC 631 ms
168,412 KB
testcase_31 AC 574 ms
160,124 KB
testcase_32 AC 1,052 ms
196,844 KB
testcase_33 AC 1,817 ms
319,448 KB
testcase_34 AC 641 ms
171,872 KB
testcase_35 AC 1,436 ms
258,800 KB
testcase_36 AC 246 ms
90,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit, stdin
from collections import defaultdict, deque, Counter
from itertools import permutations, combinations, product
from functools import lru_cache
from bisect import bisect_left, bisect_right
from heapq import heappush, heappop
from copy import copy, deepcopy
from decimal import Decimal
from random import random, randrange

# from pypyjit import set_param
# set_param('max_unroll_recursion=-1')

setrecursionlimit(1 << 20)
readline = stdin.readline
INF = 10 ** 18
MOD = 998244353
# MOD = 1000000007
ALP = 26
''' Input '''
def I(): return int(readline())
def ST(): return readline()[:-1]
def LI(): return list(map(int, readline().split()))
def LII(): return list(map(lambda x: int(x) - 1, readline().split()))
def LF(x, func): return [func() for _ in [0] * x]
def SPI(): return map(int, readline().split())
def SPII(): return map(lambda x: int(x) - 1, readline().split())
def FIE(x): return [readline()[:-1] for _ in [0] * x]

''' Array '''
def cmin(dp, i, x):
    if x < dp[i]: dp[i] = x
def cmax(dp, i, x):
    if x > dp[i]: dp[i] = x

''' Alphabet '''
def alp_a_to_i(s): return ord(s) - ord('a')
def alp_A_to_i(s): return ord(s) - ord('A')
def alp_i_to_a(i): return chr(ord('a') + i)
def alp_i_to_A(i): return chr(ord('A') + i)

''' Other'''
def nynx(y, x, H, W): return [(y + dy, x + dx) for dy, dx in [(-1, 0), (1, 0), (0, -1), (0, 1)] if 0 <= y + dy < H and 0 <= x + dx < W]
def gen(x, *args):
    if len(args) == 1: return [x] * args[0]
    if len(args) == 2: return [[x] * args[1] for _ in [0] * args[0]]
    if len(args) == 3: return [[[x] * args[2] for _ in [0] * args[1]] for _ in [0] * args[0]]
    if len(args) == 4: return [[[[x] * args[3] for _ in [0] * args[2]] for _ in [0] * args[1]] for _ in [0] * args[0]]

''' Output '''
def pprint(E): 
    print()
    for e in E: print(e)
def Yes(): print("Yes")
def No(): print("No")
def YES(): print("YES")
def NO(): print("NO")
def yn(x): print("Yes" if x else "No")
def YN(x): print("YES" if x else "NO")
###############################################################################################



H, W = SPI()
A = LF(H - 2, LI)
A = [[-1] * W] + A + [[-1] * W]

def f(i, j):
    return i * W + j

def inside(y, x): return 0 <= y < H and 0 <= x < W


node = [[] for _ in range(H * W)]
for i in range(1, H - 1):
    for j in range(W):
        if A[i][j] == -1: continue
        for dy in [-1, 0, 1]:
            for dx in [-1, 0, 1]:
                if dy == 0 and dx == 0: continue
                ny = i + dy
                nx = j + dx
                if inside(ny, nx) and A[ny][nx] != -1:
                    node[f(i, j)].append((f(ny, nx), A[ny][nx]))


dist = [INF] * (H * W)
done = [0] * (H * W)
hq = []
for i in range(1, H - 1):
    if A[i][0] != -1:
        heappush(hq, (A[i][0], f(i, 0)))
        dist[f(i, 0)] = A[i][0]

while hq:
    c, v = heappop(hq)
    if done[v]: continue
    done[v] = 1
    for to, cost in node[v]: 
        new_cost = dist[v] + cost
        if not done[to] and new_cost < dist[to]:
            dist[to] = new_cost
            heappush(hq, (dist[to], to))

ans = INF
for i in range(1, H - 1):
    ans = min(ans, dist[f(i, W - 1)])

if ans == INF: ans = -1
print(ans)
0