結果

問題 No.859 路線A、路線B、路線C
ユーザー gew1fw
提出日時 2025-06-12 17:10:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,779 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 54,244 KB
最終ジャッジ日時 2025-06-12 17:10:20
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

x, y, z = map(int, input().split())
S0, t0 = input().split()
t0 = int(t0)
S1, t1 = input().split()
t1 = int(t1)

if S0 == S1:
    print(abs(t0 - t1))
else:
    transfer_points = [
        ('A1', 'A', 1),
        ('Ax', 'A', x),
        ('B1', 'B', 1),
        ('By', 'B', y),
        ('C1', 'C', 1),
        ('Cz', 'C', z),
    ]

    min_cost = float('inf')

    for T in transfer_points:
        T_name, T_route, T_num = T
        T_is_start = (T_num == 1) if T_route in ['A', 'B', 'C'] else False

        for U in transfer_points:
            U_name, U_route, U_num = U
            if T == U:
                continue

            U_is_start = (U_num == 1) if U_route in ['A', 'B', 'C'] else False

            if (T_is_start and U_is_start) or (not T_is_start and not U_is_start):
                # Compute cost from S0 to T
                if T_route == S0:
                    cost_S0_T = abs(t0 - T_num)
                else:
                    if S0 == 'A':
                        s0_start = 1
                        s0_end = x
                    elif S0 == 'B':
                        s0_start = 1
                        s0_end = y
                    else:
                        s0_start = 1
                        s0_end = z

                    cost_start = abs(t0 - s0_start) + 1
                    if T_is_start:
                        cost_start += 0
                    else:
                        if T_route == 'A':
                            cost_start += T_num - 1
                        elif T_route == 'B':
                            cost_start += T_num - 1
                        else:
                            cost_start += T_num - 1

                    cost_end = abs(t0 - s0_end) + 1
                    if not T_is_start:
                        cost_end += 0
                    else:
                        if T_route == 'A':
                            end_num = x
                        elif T_route == 'B':
                            end_num = y
                        else:
                            end_num = z
                        cost_end += abs(T_num - end_num)

                    cost_S0_T = min(cost_start, cost_end)

                # Compute cost from U to S1
                if U_route == S1:
                    cost_U_S1 = abs(t1 - U_num)
                else:
                    if S1 == 'A':
                        s1_start = 1
                        s1_end = x
                    elif S1 == 'B':
                        s1_start = 1
                        s1_end = y
                    else:
                        s1_start = 1
                        s1_end = z

                    cost_start_u = abs(t1 - s1_start) + 1
                    if U_is_start:
                        cost_start_u += 0
                    else:
                        if U_route == 'A':
                            cost_start_u += U_num - 1
                        elif U_route == 'B':
                            cost_start_u += U_num - 1
                        else:
                            cost_start_u += U_num - 1

                    cost_end_u = abs(t1 - s1_end) + 1
                    if not U_is_start:
                        cost_end_u += 0
                    else:
                        if U_route == 'A':
                            end_num_u = x
                        elif U_route == 'B':
                            end_num_u = y
                        else:
                            end_num_u = z
                        cost_end_u += abs(U_num - end_num_u)

                    cost_U_S1 = min(cost_start_u, cost_end_u)

                total_cost = cost_S0_T + 1 + cost_U_S1
                if total_cost < min_cost:
                    min_cost = total_cost

    print(min_cost)
0