結果

問題 No.17 2つの地点に泊まりたい
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-10-25 00:37:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 67 ms / 5,000 ms
コード長 758 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 8,724 KB
最終ジャッジ日時 2023-09-25 11:24:03
合計ジャッジ時間 2,757 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,488 KB
testcase_01 AC 20 ms
8,520 KB
testcase_02 AC 21 ms
8,608 KB
testcase_03 AC 63 ms
8,652 KB
testcase_04 AC 24 ms
8,576 KB
testcase_05 AC 45 ms
8,652 KB
testcase_06 AC 36 ms
8,596 KB
testcase_07 AC 32 ms
8,656 KB
testcase_08 AC 65 ms
8,652 KB
testcase_09 AC 67 ms
8,704 KB
testcase_10 AC 57 ms
8,636 KB
testcase_11 AC 65 ms
8,608 KB
testcase_12 AC 20 ms
8,448 KB
testcase_13 AC 20 ms
8,632 KB
testcase_14 AC 19 ms
8,520 KB
testcase_15 AC 20 ms
8,592 KB
testcase_16 AC 19 ms
8,644 KB
testcase_17 AC 25 ms
8,516 KB
testcase_18 AC 55 ms
8,724 KB
testcase_19 AC 49 ms
8,624 KB
testcase_20 AC 22 ms
8,608 KB
testcase_21 AC 20 ms
8,560 KB
testcase_22 AC 57 ms
8,648 KB
testcase_23 AC 46 ms
8,568 KB
testcase_24 AC 52 ms
8,668 KB
testcase_25 AC 20 ms
8,572 KB
testcase_26 AC 61 ms
8,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heappop, heappush

N = int(input())

stopCost = [int(input()) for _ in range(N)]

M = int(input())
minDist = [[float('inf')] * N for _ in range(N)]

for _ in range(M):
    fr, to, cost = map(int, input().split())
    minDist[fr][to] = cost
    minDist[to][fr] = cost

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = minDist[i][k] + minDist[k][j]
            if minDist[i][j] > d:
                minDist[i][j] = d

ans = float('inf')
for i in range(1, N - 1):
    for j in range(i + 1, N - 1):
        dist = min(minDist[0][i] + minDist[j][N - 1], minDist[N - 1][i] + minDist[j][0]) + minDist[i][j]
        ans = min(ans, dist + stopCost[i] + stopCost[j])

print(ans)
0