結果

問題 No.807 umg tours
ユーザー stngstng
提出日時 2022-07-02 18:47:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,234 ms / 4,000 ms
コード長 1,617 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 180,888 KB
最終ジャッジ日時 2023-08-18 10:29:49
合計ジャッジ時間 32,868 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
76,232 KB
testcase_01 AC 90 ms
76,248 KB
testcase_02 AC 101 ms
76,064 KB
testcase_03 AC 96 ms
75,780 KB
testcase_04 AC 91 ms
75,884 KB
testcase_05 AC 87 ms
75,900 KB
testcase_06 AC 101 ms
76,544 KB
testcase_07 AC 96 ms
75,948 KB
testcase_08 AC 75 ms
71,168 KB
testcase_09 AC 79 ms
71,208 KB
testcase_10 AC 77 ms
71,212 KB
testcase_11 AC 1,497 ms
136,092 KB
testcase_12 AC 1,775 ms
136,424 KB
testcase_13 AC 2,284 ms
152,648 KB
testcase_14 AC 1,140 ms
113,552 KB
testcase_15 AC 892 ms
105,716 KB
testcase_16 AC 2,370 ms
157,956 KB
testcase_17 AC 3,047 ms
175,568 KB
testcase_18 AC 2,921 ms
173,804 KB
testcase_19 AC 2,734 ms
169,016 KB
testcase_20 AC 1,298 ms
127,828 KB
testcase_21 AC 1,353 ms
131,544 KB
testcase_22 AC 647 ms
99,824 KB
testcase_23 AC 587 ms
96,456 KB
testcase_24 AC 1,437 ms
172,200 KB
testcase_25 AC 3,234 ms
180,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
from sys import stdin

n,m = map(int,input().split())

abcs = [[] for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    abcs[a].append([b,c])
    abcs[b].append([a,c])

INF = 10**15

dp = [[INF]*2 for i in range(n)]
dp[0][0] = 0
dp[0][1] = 0

q = [(0, 0, 0)]#c, idx, use
while q:
    c, j, use = heappop(q)
    abc = abcs[j]
    if dp[j][use] != c:
        continue
    for k in range(len(abc)):
        j1,c1 = abc[k]
        ndis = c1 + c
        if use == 0:
            if dp[j1][0] > ndis:
                dp[j1][0] = ndis
                heappush(q, (ndis, j1, 0))
            if dp[j1][1] > c:
                dp[j1][1] = c
                heappush(q, (c, j1, 1))
        else:
            #print(c1,j1,k)
            if dp[j1][1] > ndis:
                dp[j1][1] = ndis
                heappush(q, (ndis, j1, 1))

#print(dp)

def dijkstra(s, n): # (始点, ノード数)
    dist = [INF] * n
    hq = [(0, s)] # (distance, node)
    dist[s] = 0
    seen = [False] * n # ノードが確定済みかどうか
    while hq:
        tmp = heappop(hq) # ノードを pop する
        v = tmp[1]
        c = tmp[0]
        seen[v] = True
        if dist[v] < c:
            continue
        for to, cost in abcs[v]: # ノード v に隣接しているノードに対して
            if seen[to] == False and dist[v] + cost < dist[to]:
                dist[to] = dist[v] + cost
                heappush(hq, (dist[to], to))
    return dist

fst = dijkstra(0,n)
#print(fst)
print(0)
for i in range(1,n):
    print(fst[i]+dp[i][1])
0