結果

問題 No.859 路線A、路線B、路線C
ユーザー Coki628Coki628
提出日時 2020-08-12 18:36:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 44 ms / 1,000 ms
コード長 2,614 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 53,888 KB
最終ジャッジ日時 2024-04-18 00:48:39
合計ジャッジ時間 1,652 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,248 KB
testcase_01 AC 38 ms
53,248 KB
testcase_02 AC 43 ms
53,632 KB
testcase_03 AC 42 ms
53,504 KB
testcase_04 AC 41 ms
53,760 KB
testcase_05 AC 39 ms
53,248 KB
testcase_06 AC 43 ms
53,376 KB
testcase_07 AC 41 ms
53,632 KB
testcase_08 AC 40 ms
53,376 KB
testcase_09 AC 40 ms
53,632 KB
testcase_10 AC 39 ms
53,632 KB
testcase_11 AC 39 ms
53,888 KB
testcase_12 AC 42 ms
53,376 KB
testcase_13 AC 42 ms
53,632 KB
testcase_14 AC 44 ms
53,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

def dijkstra(nodes: list, src: int) -> list:
    """ ダイクストラ高速化版(隣接リスト(0-indexed), 始点) """
    from heapq import heappush, heappop

    N = len(nodes)
    res = [INF] * N
    que = [src]
    res[src] = 0
    while que:
        cur = heappop(que)
        dist = cur // N
        cur %= N
        if res[cur] < dist:
            continue
        for nxt, cost in nodes[cur]:
            if dist + cost < res[nxt]:
                res[nxt] = dist + cost
                heappush(que, (dist+cost)*N+nxt)
    return res

x, y, z = MAP()
s1, t1 = input().split()
s2, t2 = input().split()
t1 = int(t1)
t2 = int(t2)

nodes = [[] for i in range(8)]

nodes[0].append((1, x-1))
nodes[1].append((0, x-1))
nodes[2].append((3, y-1))
nodes[3].append((2, y-1))
nodes[4].append((5, z-1))
nodes[5].append((4, z-1))

for i in range(0, 6, 2):
    nodes[i].append(((i+2)%6, 1))
    nodes[(i+2)%6].append((i, 1))
    nodes[i+1].append(((i+3)%6, 1))
    nodes[(i+3)%6].append((i+1, 1))

if s1 == s2:
    nodes[6].append((7, abs(t1 - t2)))
    nodes[7].append((6, abs(t1 - t2)))

if s1 == 'A':
    nodes[0].append((6, t1-1))
    nodes[6].append((0, t1-1))
    nodes[1].append((6, x-t1))
    nodes[6].append((1, x-t1))
elif s1 == 'B':
    nodes[2].append((6, t1-1))
    nodes[6].append((2, t1-1))
    nodes[3].append((6, y-t1))
    nodes[6].append((3, y-t1))
else:
    nodes[4].append((6, t1-1))
    nodes[6].append((4, t1-1))
    nodes[5].append((6, z-t1))
    nodes[6].append((5, z-t1))

if s2 == 'A':
    nodes[0].append((7, t2-1))
    nodes[7].append((0, t2-1))
    nodes[1].append((7, x-t2))
    nodes[7].append((1, x-t2))
elif s2 == 'B':
    nodes[2].append((7, t2-1))
    nodes[7].append((2, t2-1))
    nodes[3].append((7, y-t2))
    nodes[7].append((3, y-t2))
else:
    nodes[4].append((7, t2-1))
    nodes[7].append((4, t2-1))
    nodes[5].append((7, z-t2))
    nodes[7].append((5, z-t2))

ans = dijkstra(nodes, 6)[7]
print(ans)
0