結果

問題 No.859 路線A、路線B、路線C
ユーザー qwewe
提出日時 2025-05-14 13:26:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,084 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 54,600 KB
最終ジャッジ日時 2025-05-14 13:27:19
合計ジャッジ時間 1,365 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def solve():
    # x, y, z are max station numbers, not directly used in cost calculation
    # as transfers are based on station 1 or direct target.
    x, y, z = map(int, input().split()) 
    s0_line_char, s0_station_str = input().split()
    s1_line_char, s1_station_str = input().split()

    s0_station = int(s0_station_str)
    s1_station = int(s1_station_str)

    ans = float('inf')

    # Path 0: Direct travel on the same line
    if s0_line_char == s1_line_char:
        ans = min(ans, abs(s0_station - s1_station))

    # Helper function for the cost of a single transfer segment.
    # Calculates cost from (l1, n1) to (l2, n2) assuming l1 != l2.
    def get_transfer_segment_cost(l1, n1, l2, n2):
        # Standard transfer (A-B, A-C):
        # Path: (l1, n1) -> l1_station_1 --(1 yen hop)--> l2_station_1 -> (l2, n2)
        if (l1 == 'A' and l2 == 'B') or \
           (l1 == 'B' and l2 == 'A'):
            return abs(n1 - 1) + 1 + abs(1 - n2)
        
        if (l1 == 'A' and l2 == 'C') or \
           (l1 == 'C' and l2 == 'A'):
            return abs(n1 - 1) + 1 + abs(1 - n2)

        # Special B-C transfer:
        # Path: (l1, n1) -> l1_station_1 --(1 yen hop)--> (l2, n2 directly)
        # Cost includes travel to station 1 on l1, and the 1 yen transfer hop.
        # Arrival is directly at station n2 on line l2.
        if (l1 == 'B' and l2 == 'C'): # From (B, n1) to (C, n2)
            return abs(n1 - 1) + 1
        
        if (l1 == 'C' and l2 == 'B'): # From (C, n1) to (B, n2)
            return abs(n1 - 1) + 1
        
        # Should not be reached if l1 != l2 for lines A, B, C.
        return float('inf') 

    # Path 1: One transfer hop: s0_line -> s1_line
    if s0_line_char != s1_line_char:
        cost_path1 = get_transfer_segment_cost(s0_line_char, s0_station, s1_line_char, s1_station)
        ans = min(ans, cost_path1)

    # Path 2: Two transfer hops: s0_line -> inter_line -> s1_line
    # This path is relevant if s0_line is different from s1_line.
    if s0_line_char != s1_line_char:
        all_lines = ['A', 'B', 'C']
        for inter_line_char in all_lines:
            # The intermediate line must be different from the start and end lines.
            if inter_line_char == s0_line_char or inter_line_char == s1_line_char:
                continue
            
            # Cost from (s0_line, s0_station) to (inter_line_char, 1)
            # The '1' signifies arriving at station 1 of the intermediate line.
            cost_to_intermediate = get_transfer_segment_cost(s0_line_char, s0_station, inter_line_char, 1)
            
            # Cost from (inter_line_char, 1) to (s1_line, s1_station)
            # Starting from station 1 of the intermediate line.
            cost_from_intermediate = get_transfer_segment_cost(inter_line_char, 1, s1_line_char, s1_station)
            
            if cost_to_intermediate != float('inf') and cost_from_intermediate != float('inf'):
                ans = min(ans, cost_to_intermediate + cost_from_intermediate)
            
    print(int(ans))

solve()
0