結果

問題 No.17 2つの地点に泊まりたい
ユーザー mkawa2mkawa2
提出日時 2020-01-03 11:28:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 312 ms / 5,000 ms
コード長 1,571 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 27,000 KB
最終ジャッジ日時 2023-08-14 19:07:44
合計ジャッジ時間 3,565 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,616 KB
testcase_01 AC 20 ms
8,816 KB
testcase_02 AC 20 ms
8,724 KB
testcase_03 AC 29 ms
9,888 KB
testcase_04 AC 32 ms
10,208 KB
testcase_05 AC 166 ms
18,844 KB
testcase_06 AC 84 ms
14,368 KB
testcase_07 AC 61 ms
12,884 KB
testcase_08 AC 312 ms
25,600 KB
testcase_09 AC 306 ms
27,000 KB
testcase_10 AC 62 ms
12,484 KB
testcase_11 AC 123 ms
17,208 KB
testcase_12 AC 20 ms
8,692 KB
testcase_13 AC 21 ms
8,776 KB
testcase_14 AC 20 ms
8,776 KB
testcase_15 AC 19 ms
8,688 KB
testcase_16 AC 20 ms
8,688 KB
testcase_17 AC 21 ms
8,712 KB
testcase_18 AC 26 ms
9,524 KB
testcase_19 AC 23 ms
9,032 KB
testcase_20 AC 20 ms
8,876 KB
testcase_21 AC 20 ms
8,840 KB
testcase_22 AC 28 ms
9,624 KB
testcase_23 AC 125 ms
17,324 KB
testcase_24 AC 118 ms
16,848 KB
testcase_25 AC 22 ms
8,888 KB
testcase_26 AC 219 ms
21,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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()
0