結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  rlangevin | 
| 提出日時 | 2023-09-19 12:05:04 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,381 bytes | 
| コンパイル時間 | 658 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 649,728 KB | 
| 最終ジャッジ日時 | 2024-07-05 21:40:41 | 
| 合計ジャッジ時間 | 15,571 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 TLE * 1 MLE * 1 -- * 8 | 
ソースコード
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)
            
            
            
        