結果

問題 No.34 砂漠の行商人
ユーザー rlangevinrlangevin
提出日時 2023-09-19 12:05:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,381 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 289,232 KB
最終ジャッジ日時 2023-09-19 12:05:22
合計ジャッジ時間 17,179 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,608 KB
testcase_01 AC 21 ms
8,688 KB
testcase_02 AC 88 ms
12,720 KB
testcase_03 AC 176 ms
17,252 KB
testcase_04 AC 1,812 ms
94,264 KB
testcase_05 AC 2,109 ms
114,776 KB
testcase_06 AC 108 ms
13,428 KB
testcase_07 AC 3,645 ms
173,296 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
inf = 10 ** 18
def bfs(G, s, N):
    Q = deque([])
    dist = [inf] * N
    par = [-1] * N
    sz = [1] * N
    dist[s] = 0
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        sz[u] += sz[s]
        Q.append(u)
 
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != inf:
                continue
            dist[v] = dist[u] + 1
            par[v] = u
            sz[v] += sz[u]
            Q.append(v)
            
    return dist


N, V, sx, sy, gx, gy = map(int, input().split())
sx, sy, gx, gy = sx-1, sy-1, gx-1, gy-1
L = []
for i in range(N):
    L.append(list(map(int, input().split())))

M = N * N * (V + 1)
if V >= 2000:
    print(abs(sx - gx) + abs(sy - gy))
    exit()

def f(x, y, v):
    return x + y * N + v * N * N

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
G = [[] for i in range(M)]
for i in range(N):
    for j in range(N):
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > N - 1 or y < 0 or y > N - 1:
                continue
            for v in range(1, V + 1):
                if v - L[x][y] <= 0:
                    continue
                G[f(i, j, v)].append(f(x, y, v - L[x][y]))

D = bfs(G, f(sy, sx, V), M)
ans = inf
for v in range(1, V + 1):
    ans = min(ans, D[f(gy, gx, v)])

print(ans) if ans != inf else print(-1)
0