結果

問題 No.17 2つの地点に泊まりたい
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-03-01 13:38:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 96 ms / 5,000 ms
コード長 1,288 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-23 02:10:39
合計ジャッジ時間 2,391 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 29 ms
11,008 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 96 ms
11,008 KB
testcase_04 AC 34 ms
11,008 KB
testcase_05 AC 68 ms
11,008 KB
testcase_06 AC 52 ms
10,880 KB
testcase_07 AC 45 ms
11,136 KB
testcase_08 AC 96 ms
11,008 KB
testcase_09 AC 95 ms
11,008 KB
testcase_10 AC 86 ms
11,008 KB
testcase_11 AC 95 ms
11,008 KB
testcase_12 AC 28 ms
11,008 KB
testcase_13 AC 28 ms
11,008 KB
testcase_14 AC 28 ms
10,880 KB
testcase_15 AC 27 ms
10,880 KB
testcase_16 AC 27 ms
10,880 KB
testcase_17 AC 34 ms
10,880 KB
testcase_18 AC 84 ms
10,880 KB
testcase_19 AC 79 ms
11,008 KB
testcase_20 AC 33 ms
10,880 KB
testcase_21 AC 30 ms
10,880 KB
testcase_22 AC 93 ms
11,008 KB
testcase_23 AC 68 ms
10,880 KB
testcase_24 AC 79 ms
11,008 KB
testcase_25 AC 29 ms
11,008 KB
testcase_26 AC 83 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
import itertools


INF = 10 ** 9


class WarshallFloyd(object):

    def __init__(self, max_v, edges):
        self.max_v = max_v
        self.dist = edges
        for v in range(max_v):
            self.dist[v][v] = 0

    def compute(self):
        for v, u, w in itertools.product(range(self.max_v), repeat=3):
            new_length = self.dist[u][v] + self.dist[v][w]
            self.dist[u][w] = min(self.dist[u][w], new_length)
        return self.dist


def main():
    n = int(input())
    stay_costs = array.array("I", (int(input()) for _ in range(n)))
    m = int(input())
    edges = [array.array("L", (INF for _ in range(n))) for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        edges[a][b] = c
        edges[b][a] = c
    wf = WarshallFloyd(n, edges)
    move_costs = wf.compute()
    answer = INF
    for u0, v0 in itertools.combinations(range(1, n - 1), 2):
        for u, v in [(u0, v0), (v0, u0)]:
            move_cost = move_costs[0][u] + \
                move_costs[u][v] + move_costs[v][n - 1]
            stay_cost = stay_costs[u] + stay_costs[v]
            answer = min(answer, move_cost + stay_cost)
    print(answer)


if __name__ == '__main__':
    main()
0