結果

問題 No.807 umg tours
ユーザー roarisroaris
提出日時 2019-08-04 19:27:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,510 ms / 4,000 ms
コード長 1,784 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 154,628 KB
最終ジャッジ日時 2024-11-23 19:35:06
合計ジャッジ時間 22,692 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
61,312 KB
testcase_01 AC 55 ms
62,720 KB
testcase_02 AC 62 ms
65,024 KB
testcase_03 AC 55 ms
63,488 KB
testcase_04 AC 54 ms
61,440 KB
testcase_05 AC 54 ms
61,184 KB
testcase_06 AC 57 ms
63,616 KB
testcase_07 AC 60 ms
65,792 KB
testcase_08 AC 45 ms
54,144 KB
testcase_09 AC 44 ms
54,656 KB
testcase_10 AC 43 ms
54,912 KB
testcase_11 AC 942 ms
112,576 KB
testcase_12 AC 1,350 ms
120,956 KB
testcase_13 AC 1,724 ms
130,204 KB
testcase_14 AC 755 ms
101,752 KB
testcase_15 AC 585 ms
95,704 KB
testcase_16 AC 1,615 ms
132,032 KB
testcase_17 AC 2,357 ms
148,112 KB
testcase_18 AC 2,157 ms
146,216 KB
testcase_19 AC 2,095 ms
141,604 KB
testcase_20 AC 1,019 ms
118,756 KB
testcase_21 AC 1,079 ms
119,872 KB
testcase_22 AC 528 ms
93,896 KB
testcase_23 AC 445 ms
90,180 KB
testcase_24 AC 1,118 ms
147,712 KB
testcase_25 AC 2,510 ms
154,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
from collections import defaultdict
import sys
input = sys.stdin.readline

def dijkstra(start):
    distance = [[10**18 for _ in range(2)] for _ in range(N+1)]
    distance[start][0] = 0
    distance[start][1] = 0
    priority_queue = []
    
    heappush(priority_queue, (distance[start][0], start, 0))
    heappush(priority_queue, (distance[start][1], start, 1))
    
    while len(priority_queue) > 0:
        current_dist, current_node, state = heappop(priority_queue)
        
        if distance[current_node][state] < current_dist:
            continue
        
        if state == 0:
            for next_node, w in adj_dict[current_node]:
                if distance[next_node][0] > distance[current_node][0] + w:
                    distance[next_node][0] = distance[current_node][0] + w
                    heappush(priority_queue, (distance[next_node][0], next_node, 0))
                if distance[next_node][1] > distance[current_node][0]:
                    distance[next_node][1] = distance[current_node][0]
                    heappush(priority_queue, (distance[next_node][1], next_node, 1))
        else:
            for next_node, w in adj_dict[current_node]:
                if distance[next_node][1] > distance[current_node][1] + w:
                    distance[next_node][1] = distance[current_node][1] + w
                    heappush(priority_queue, (distance[next_node][1], next_node, 1))
    
    return distance

N, M = map(int, input().split())
adj_dict = defaultdict(list)

for _ in range(M):
    a_i, b_i, c_i = map(int, input().split())
    adj_dict[a_i].append((b_i, c_i))
    adj_dict[b_i].append((a_i, c_i))

distance = dijkstra(1)

for i in range(1, N+1):
    ans = distance[i][0] + distance[i][1]
    print(ans)
0