結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
mkawa2
|
| 提出日時 | 2020-01-03 11:28:45 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 300 ms / 5,000 ms |
| コード長 | 1,571 bytes |
| コンパイル時間 | 96 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 29,200 KB |
| 最終ジャッジ日時 | 2024-11-22 18:58:20 |
| 合計ジャッジ時間 | 3,372 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
from heapq import *
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def main():
to=defaultdict(list)
n=II()
cost_node=[II() for _ in range(n)]
m=II()
for _ in range(m):
a,b,c=MI()
to[a].append([b,c])
to[b].append([a,c])
costs=[[-1]*n for _ in range(3)]
hp0=[]
hp1=[]
hp2=[]
heappush(hp0,[0,0])
cnt=0
while hp0 and cnt<n:
c,u=heappop(hp0)
if costs[0][u]!=-1:continue
costs[0][u]=c
cnt+=1
for v,dc in to[u]:
if u!=0 and u!=n-1:heappush(hp1,[c+dc+cost_node[u],v,u])
if costs[0][v]!=-1:continue
heappush(hp0,[c+dc,v])
fin1=set()
while hp1:
c,u,fin=heappop(hp1)
if (u,fin) in fin1:continue
fin1.add((u,fin))
for v,dc in to[u]:
if u!=0 and u!=n-1 and u!=fin:
heappush(hp2,[c+dc+cost_node[u],v])
if (v,fin) in fin1:continue
heappush(hp1,[c+dc,v,fin])
while hp2:
c,u=heappop(hp2)
if u==n-1:
print(c)
exit()
if costs[2][u]!=-1:continue
costs[2][u]=c
for v,dc in to[u]:
if costs[2][v]!=-1:continue
heappush(hp2,[c+dc,v])
main()
mkawa2