結果

問題 No.17 2つの地点に泊まりたい
ユーザー lloyzlloyz
提出日時 2022-03-12 12:43:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 926 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 11,076 KB
実行使用メモリ 8,972 KB
最終ジャッジ日時 2023-10-15 01:13:46
合計ジャッジ時間 2,268 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,656 KB
testcase_01 AC 20 ms
8,544 KB
testcase_02 AC 20 ms
8,624 KB
testcase_03 AC 26 ms
8,804 KB
testcase_04 AC 25 ms
8,588 KB
testcase_05 AC 48 ms
8,732 KB
testcase_06 AC 37 ms
8,736 KB
testcase_07 AC 33 ms
8,676 KB
testcase_08 AC 64 ms
8,928 KB
testcase_09 AC 66 ms
8,972 KB
testcase_10 AC 35 ms
8,736 KB
testcase_11 AC 46 ms
8,772 KB
testcase_12 AC 20 ms
8,612 KB
testcase_13 AC 20 ms
8,692 KB
testcase_14 AC 20 ms
8,696 KB
testcase_15 AC 20 ms
8,692 KB
testcase_16 AC 20 ms
8,556 KB
testcase_17 AC 21 ms
8,680 KB
testcase_18 AC 25 ms
8,768 KB
testcase_19 AC 23 ms
8,600 KB
testcase_20 AC 21 ms
8,628 KB
testcase_21 AC 20 ms
8,708 KB
testcase_22 AC 26 ms
8,680 KB
testcase_23 AC 44 ms
8,876 KB
testcase_24 AC 43 ms
8,856 KB
testcase_25 AC 21 ms
8,612 KB
testcase_26 AC 56 ms
8,848 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