結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
76,152 KB
testcase_01 AC 102 ms
76,740 KB
testcase_02 AC 127 ms
78,124 KB
testcase_03 AC 150 ms
78,580 KB
testcase_04 AC 374 ms
125,060 KB
testcase_05 AC 392 ms
135,900 KB
testcase_06 AC 125 ms
78,236 KB
testcase_07 AC 604 ms
167,524 KB
testcase_08 AC 1,026 ms
225,832 KB
testcase_09 AC 106 ms
77,024 KB
testcase_10 AC 103 ms
76,776 KB
testcase_11 AC 107 ms
76,888 KB
testcase_12 AC 373 ms
123,912 KB
testcase_13 AC 107 ms
76,920 KB
testcase_14 AC 104 ms
76,840 KB
testcase_15 AC 106 ms
76,836 KB
testcase_16 AC 105 ms
76,820 KB
testcase_17 MLE -
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