結果

問題 No.2213 Neq Move
ユーザー ShirotsumeShirotsume
提出日時 2022-10-12 15:47:50
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,376 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 79,776 KB
最終ジャッジ日時 2023-09-21 21:56:46
合計ジャッジ時間 2,672 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
76,204 KB
testcase_01 WA -
testcase_02 AC 350 ms
79,644 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def dijkstra(s, n, graph):
    INF = 10 ** 18
    import heapq
    dist = [INF] * n
    dist[s] = 0
    bef = [0] * n
    bef[s] = s
    hq = [(0, s)]
    heapq.heapify(hq)
    visit = [False] * n
    while hq:
        c, v = heapq.heappop(hq)
        visit[v] = True
        if c > dist[v]:
            continue
        for to, cost in graph[v]:
            if visit[to] == False and dist[v] + cost < dist[to]:
                dist[to] =  cost + dist[v]
                bef[to] = v
                heapq.heappush(hq, (dist[to], to))
    return dist, bef
def solve(a, b, c, d):
    ind = {}
    i = 0
    for v in [0, 1, a, b, c, d]:
        for u in [0, 1, a, b, c, d]:
            if u != v:
                ind[u, v] = i
                i += 1
    graph = [[] for _ in range(i)]
    for u1, v1 in ind.keys():
        i1 = ind[u1, v1]
        for u2, v2 in ind.keys():
            i2 = ind[u2, v2]
            if u1 <= u2 and v1 <= v2 and i1 != i2 and ((u1 < v1 and u2 < v2) or (u1 > v1 and u2 > v2)):
                graph[i1].append((i2, abs(u2 - u1) + abs(v2 - v1)))
            if (u2 == 0 and v1 == v2) or (v2 == 0 and u1 == u2):
                graph[i1].append((i2, 1))
    D, _ = dijkstra(ind[a, b], i, graph)
    return (D[ind[(c, d)]])

                

    




for _ in range(int(input())):
    a, b, c, d = map(int,input().split())
    print(solve(a, b, c, d))
0