結果

問題 No.17 2つの地点に泊まりたい
ユーザー lloyzlloyz
提出日時 2022-03-12 12:43:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 200 ms / 5,000 ms
コード長 926 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 78,700 KB
最終ジャッジ日時 2023-10-15 01:13:33
合計ジャッジ時間 6,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,168 KB
testcase_01 AC 98 ms
71,596 KB
testcase_02 AC 96 ms
71,352 KB
testcase_03 AC 169 ms
78,048 KB
testcase_04 AC 142 ms
77,804 KB
testcase_05 AC 176 ms
78,428 KB
testcase_06 AC 165 ms
77,912 KB
testcase_07 AC 161 ms
77,768 KB
testcase_08 AC 200 ms
78,700 KB
testcase_09 AC 189 ms
78,460 KB
testcase_10 AC 169 ms
78,308 KB
testcase_11 AC 167 ms
77,928 KB
testcase_12 AC 97 ms
71,100 KB
testcase_13 AC 96 ms
71,452 KB
testcase_14 AC 97 ms
71,252 KB
testcase_15 AC 97 ms
71,468 KB
testcase_16 AC 99 ms
71,408 KB
testcase_17 AC 101 ms
72,176 KB
testcase_18 AC 141 ms
77,996 KB
testcase_19 AC 128 ms
77,596 KB
testcase_20 AC 99 ms
71,256 KB
testcase_21 AC 97 ms
71,500 KB
testcase_22 AC 149 ms
78,096 KB
testcase_23 AC 178 ms
77,956 KB
testcase_24 AC 177 ms
78,020 KB
testcase_25 AC 110 ms
76,736 KB
testcase_26 AC 186 ms
78,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heappop, heappush

n = int(input())
P = [int(input()) for _ in range(n)]
m = int(input())
Edge = defaultdict(list)
for _ in range(m):
    a, b, c = map(int, input().split())
    Edge[a].append((b, c))
    Edge[b].append((a, c))

INF = 10**18
C = [[INF for _ in range(n)] for _ in range(n)]
for i in range(n):
    C[i][i] = 0
    Heap = [(0, i, -1)]
    while Heap:
        cost, curr, prev = heappop(Heap)
        if cost > C[i][curr]:
            continue
        for np, d in Edge[curr]:
            if np == prev:
                continue
            if cost + d > C[i][np]:
                continue
            C[i][np] = cost + d
            heappush(Heap, (cost + d, np, curr))

ans = INF
for i in range(1, n - 1):
    for j in range(1, n - 1):
        if i == j:
            continue
        ans = min(ans, C[0][i] + P[i] + C[i][j] + P[j] + C[j][n - 1])
print(ans)
0