結果

問題 No.34 砂漠の行商人
ユーザー rlangevinrlangevin
提出日時 2023-09-19 12:08:59
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,387 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 666,472 KB
最終ジャッジ日時 2023-09-19 12:09:12
合計ジャッジ時間 12,576 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
75,952 KB
testcase_01 AC 103 ms
76,768 KB
testcase_02 AC 129 ms
78,288 KB
testcase_03 AC 93 ms
71,800 KB
testcase_04 AC 360 ms
125,016 KB
testcase_05 AC 378 ms
135,920 KB
testcase_06 AC 129 ms
78,340 KB
testcase_07 AC 540 ms
167,592 KB
testcase_08 AC 918 ms
225,772 KB
testcase_09 AC 102 ms
76,816 KB
testcase_10 AC 103 ms
77,048 KB
testcase_11 AC 101 ms
76,816 KB
testcase_12 AC 361 ms
123,896 KB
testcase_13 AC 104 ms
76,772 KB
testcase_14 AC 99 ms
76,744 KB
testcase_15 AC 99 ms
76,792 KB
testcase_16 AC 103 ms
76,864 KB
testcase_17 AC 101 ms
76,888 KB
testcase_18 AC 96 ms
71,388 KB
testcase_19 MLE -
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
d = abs(sx - gx) + abs(sy - gy)
L = []
for i in range(N):
    L.append(list(map(int, input().split())))

M = N * N * (V + 1)
if V > d * 9:
    print(d)
    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