結果

問題 No.807 umg tours
ユーザー kurimupykurimupy
提出日時 2020-06-15 17:03:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,690 ms / 4,000 ms
コード長 1,829 bytes
コンパイル時間 480 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 199,268 KB
最終ジャッジ日時 2024-05-03 00:25:06
合計ジャッジ時間 17,892 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
61,952 KB
testcase_01 AC 47 ms
63,616 KB
testcase_02 AC 54 ms
67,584 KB
testcase_03 AC 50 ms
65,536 KB
testcase_04 AC 43 ms
61,312 KB
testcase_05 AC 50 ms
63,000 KB
testcase_06 AC 57 ms
67,832 KB
testcase_07 AC 49 ms
64,768 KB
testcase_08 AC 35 ms
53,260 KB
testcase_09 AC 36 ms
53,888 KB
testcase_10 AC 38 ms
53,632 KB
testcase_11 AC 946 ms
157,868 KB
testcase_12 AC 931 ms
143,616 KB
testcase_13 AC 1,303 ms
169,912 KB
testcase_14 AC 640 ms
116,204 KB
testcase_15 AC 508 ms
109,036 KB
testcase_16 AC 1,337 ms
175,856 KB
testcase_17 AC 1,650 ms
195,684 KB
testcase_18 AC 1,659 ms
195,664 KB
testcase_19 AC 1,598 ms
191,512 KB
testcase_20 AC 799 ms
142,268 KB
testcase_21 AC 860 ms
145,024 KB
testcase_22 AC 415 ms
105,716 KB
testcase_23 AC 378 ms
100,240 KB
testcase_24 AC 729 ms
181,940 KB
testcase_25 AC 1,690 ms
199,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
input = sys.stdin.readline


def dijkstra(s, graph):
    n = len(graph)-1
    dist = [float("inf") for i in range(n+1)]
    dist[s] = 0
    pq = []
    heapq.heapify(pq)
    heapq.heappush(pq, (0, s))
    while pq:
        mini_dis, node = heapq.heappop(pq)
        if dist[node] < mini_dis:
            continue
        for w, point in graph[node]:
            if dist[point] < w:
                continue
            newlen = dist[node]+w
            if newlen < dist[point]:
                heapq.heappush(pq, (newlen, point))
                dist[point] = newlen
    return dist


def s_dijkstra(s, graph):
    n = len(graph)-1
    dist = [float("inf") for i in range(n+1)]
    dist[s] = 0
    dist[s+N] = 0
    pq = []
    heapq.heapify(pq)
    heapq.heappush(pq, (0, s))
    heapq.heappush(pq, (0, s+N))
    while pq:
        mini_dis, node = heapq.heappop(pq)
        if dist[node] < mini_dis:
            continue
        for w, point in graph[node]:
            if dist[point] < w:
                continue
            newlen = dist[node]+w
            if newlen < dist[point]:
                heapq.heappush(pq, (newlen, point))
                dist[point] = newlen
    return dist


N, M = map(int, input().split())
graph = [[] for i in range(N+1)]
special_graph = [[] for i in range(2*N+1)]


for _ in range(M):
    a, b, c = map(int, input().split())
    graph[a].append((c, b))
    graph[b].append((c, a))

    special_graph[a].append((0, b+N))
    special_graph[a].append((c, b))
    special_graph[a+N].append((c, b+N))
    special_graph[b].append((c, a))
    special_graph[b].append((0, a+N))
    special_graph[b+N].append((c, a+N))


distance = dijkstra(1, graph)
sub_distance = s_dijkstra(1, special_graph)
for i in range(1, N+1):
    ans = distance[i]+sub_distance[i+N]
    print(ans)

0