結果

問題 No.2328 Build Walls
ユーザー tyawanmusityawanmusi
提出日時 2023-05-24 10:12:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,279 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 335,920 KB
最終ジャッジ日時 2023-08-25 02:37:53
合計ジャッジ時間 8,536 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,584 KB
testcase_01 AC 17 ms
8,252 KB
testcase_02 AC 16 ms
8,228 KB
testcase_03 AC 16 ms
8,176 KB
testcase_04 AC 18 ms
8,224 KB
testcase_05 AC 16 ms
8,152 KB
testcase_06 AC 16 ms
8,192 KB
testcase_07 AC 15 ms
8,152 KB
testcase_08 AC 18 ms
8,196 KB
testcase_09 AC 16 ms
8,144 KB
testcase_10 AC 15 ms
8,188 KB
testcase_11 AC 15 ms
8,112 KB
testcase_12 AC 16 ms
8,204 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush


def dijkstra(s, n, edge):
  inf = float("inf")
  ans = [inf] * n
  ans[s] = 0
  root = [-1] * n
  h = [[0, s]]
  while h:
    c, v = heappop(h)
    if ans[v] < c:
      continue
    for u, t in edge[v]:
      if c + t < ans[u]:
        ans[u] = c + t
        root[u] = v
        heappush(h, [c + t, u])
  return [ans, root]


h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h - 2)]
def ij(i, j): return i * w + j


edge = [[]for _ in range((h - 2) * w + 2)]
d = [
    [-1, -1],
    [-1, 0],
    [-1, 1],
    [0, -1],
    [0, 1],
    [1, -1],
    [1, 0],
    [1, 1]
]
inf = 10**10
for i in range(h - 2):
  for j in range(w):
    for di, dj in d:
      if 0 <= i + di < h - 2 and 0 <= j + dj < w:
        if a[i + di][j + dj] == -1:
          edge[ij(i, j)].append((ij(i + di, j + dj), inf))
        else:
          edge[ij(i, j)].append((ij(i + di, j + dj), a[i + di][j + dj]))
for i in range(h - 2):
  if a[i][0] == -1:
    edge[(h - 2) * w].append((ij(i, 0), inf))
  else:
    edge[(h - 2) * w].append((ij(i, 0), a[i][0]))
  edge[ij(i, w - 1)].append(((h - 2) * w + 1, 0))
ans = dijkstra((h - 2) * w, (h - 2) * w + 2, edge)[0]
if ans[(h - 2) * w + 1] >= inf: print(-1)
else: print(ans[(h - 2) * w + 1])
0