結果

問題 No.859 路線A、路線B、路線C
ユーザー convexineqconvexineq
提出日時 2021-04-04 19:59:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 81 ms / 1,000 ms
コード長 954 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 71,580 KB
最終ジャッジ日時 2023-08-27 21:25:02
合計ジャッジ時間 2,353 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,300 KB
testcase_01 AC 77 ms
71,252 KB
testcase_02 AC 78 ms
71,360 KB
testcase_03 AC 81 ms
71,256 KB
testcase_04 AC 76 ms
71,420 KB
testcase_05 AC 77 ms
71,260 KB
testcase_06 AC 76 ms
71,352 KB
testcase_07 AC 77 ms
71,580 KB
testcase_08 AC 78 ms
71,384 KB
testcase_09 AC 75 ms
71,204 KB
testcase_10 AC 75 ms
71,424 KB
testcase_11 AC 78 ms
71,340 KB
testcase_12 AC 79 ms
71,120 KB
testcase_13 AC 76 ms
71,380 KB
testcase_14 AC 78 ms
71,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
def dijkstra(g,start):
    n = len(g)
    INF = 1<<61
    dist = [INF]*(n) #startからの最短距離
    #pending = n-1 #未確定の点の個数
    dist[start] = 0
    q = [(0,start)] #(そこまでの距離、点)
    while q:# and pending:
        dv,v = heappop(q)
        if dist[v] < dv: continue
        for to, cost in g[v]:
            if dv + cost < dist[to]:
                dist[to] = dv + cost
                heappush(q, (dist[to], to))
    return dist

x,y,z = map(int, input().split())
s,a = input().split()
t,b = input().split()
g = [[] for _ in range(8)]
for p,q,r in[(0,1,1),(1,2,1),(0,2,1),(3,4,1),(3,5,1),(4,5,1),(0,3,x-1),(2,5,z-1),(1,4,y-1)]:
    g[p].append((q,r))
    g[q].append((p,r))

a = int(a)
b = int(b)
c = "ABC".find(s)
g[6] = [(c,a-1),(c+3,[x,y,z][c]-a)]
c = "ABC".find(t)
g[c].append((7,b-1))
g[c+3].append((7,[x,y,z][c]-b))

dist = dijkstra(g,6)
v = dist[7]
if s==t: v = min(v,abs(a-b))
print(v)
0