結果
| 問題 | No.17 2つの地点に泊まりたい | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-02-09 23:58:19 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 457 ms / 5,000 ms | 
| コード長 | 2,095 bytes | 
| コンパイル時間 | 241 ms | 
| コンパイル使用メモリ | 82,176 KB | 
| 実行使用メモリ | 80,044 KB | 
| 最終ジャッジ日時 | 2024-07-07 05:25:04 | 
| 合計ジャッジ時間 | 6,102 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
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])
            
            
            
        