結果

問題 No.17 2つの地点に泊まりたい
ユーザー kohei2019kohei2019
提出日時 2021-02-09 23:58:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 581 ms / 5,000 ms
コード長 2,095 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 82,116 KB
最終ジャッジ日時 2023-09-21 11:27:24
合計ジャッジ時間 9,247 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,292 KB
testcase_01 AC 74 ms
71,172 KB
testcase_02 AC 108 ms
77,740 KB
testcase_03 AC 337 ms
80,148 KB
testcase_04 AC 210 ms
79,484 KB
testcase_05 AC 432 ms
81,408 KB
testcase_06 AC 354 ms
80,372 KB
testcase_07 AC 302 ms
80,228 KB
testcase_08 AC 581 ms
81,424 KB
testcase_09 AC 568 ms
81,396 KB
testcase_10 AC 380 ms
81,872 KB
testcase_11 AC 454 ms
80,476 KB
testcase_12 AC 75 ms
71,300 KB
testcase_13 AC 74 ms
71,512 KB
testcase_14 AC 75 ms
71,616 KB
testcase_15 AC 73 ms
71,300 KB
testcase_16 AC 74 ms
71,160 KB
testcase_17 AC 141 ms
78,076 KB
testcase_18 AC 227 ms
78,736 KB
testcase_19 AC 227 ms
79,800 KB
testcase_20 AC 112 ms
77,468 KB
testcase_21 AC 76 ms
71,612 KB
testcase_22 AC 398 ms
82,116 KB
testcase_23 AC 404 ms
81,072 KB
testcase_24 AC 432 ms
81,412 KB
testcase_25 AC 141 ms
78,640 KB
testcase_26 AC 506 ms
81,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
N = int(input())
lsS = [int(input()) for i in range(N)]
M = int(input())
lsg = [[] for i in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    lsg[a].append((b,c))
    lsg[b].append((a,c))
#滞在0(普通のダイクストラ)
lscost0 = [float('INF')]*(N)
used = [False]*(N)
lscost0[0] = 0
h = [(0,0)]
heapq.heapify(h)
while h:
    cost,node = heapq.heappop(h)
    if used[node]:
        continue
    used[node] = True
    for nj,cj in lsg[node]:
        if used[nj]:
            continue
        if lscost0[nj] > lscost0[node]+cj:
            lscost0[nj] = lscost0[node]+cj
            heapq.heappush(h,(lscost0[nj],nj))
#滞在1 O(N)*(普通のダイクストラ),始点で滞在。他1か所で滞在。
lscost1 = [float('INF')]*(N)
for st in range(1,N-1):
    lscost1a = [float('INF')]*(N)
    used = [False]*(N)
    lscost1a[st] = lscost0[st]+lsS[st]
    h = [(lscost1a[st],st)]
    heapq.heapify(h)
    while h:
        cost,node = heapq.heappop(h)
        if used[node]:
            continue
        used[node] = True
        for nj,cj in lsg[node]:
            if used[nj]:
                continue
            if lscost1a[nj] > lscost1a[node]+cj:
                lscost1a[nj] = lscost1a[node]+cj
                heapq.heappush(h,(lscost1a[nj],nj))
    for st2 in range(1,N-1):
        if st2 == st:
            continue
        lscost1b = [float('INF')]*(N)
        used = [False]*(N)
        lscost1b[st2] = lscost1a[st2]+lsS[st2]
        if lscost1b[st2] == float('INF'):
            continue
        h = [(lscost1b[st2],st2)]
        heapq.heapify(h)
        while h:
            cost,node = heapq.heappop(h)
            if used[node]:
                continue
            used[node] = True
            for nj,cj in lsg[node]:
                if used[nj]:
                    continue
                if lscost1b[nj] > lscost1b[node]+cj:
                    lscost1b[nj] = lscost1b[node]+cj
                    heapq.heappush(h,(lscost1b[nj],nj))
        for i in range(N):
            lscost1[i] = min(lscost1[i],lscost1b[i])
print(lscost1[-1])
0