結果

問題 No.807 umg tours
ユーザー TakoKurageTakoKurage
提出日時 2019-03-22 22:37:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,681 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 227,476 KB
最終ジャッジ日時 2023-10-19 09:51:49
合計ジャッジ時間 13,452 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heappop, heappush


class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    def __init__(self, graph, start):
        self.g = graph.graph
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0
        self.prev = defaultdict(lambda: None)
        self.Q = []
        heappush(self.Q, (self.dist[start], start))
        while self.Q:
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        return self.dist[goal]


N, M = [int(i) for i in input().split()]
graphs = [Graph() for _ in range(M + 1)]
inputs = [(int(i) for i in input().split()) for _ in range(M)]

for i, (a, b, c) in enumerate(inputs):
    for j, graph in enumerate(graphs):
        if i == j:
            graph.add_edge(a, b, 0)
            graph.add_edge(b, a, 0)
        else:
            graph.add_edge(a, b, c)
            graph.add_edge(b, a, c)

for j in range(N):
    result = Dijkstra(graphs[0], 1).shortest_distance(
        j + 1) + min([Dijkstra(graph, 1).shortest_distance(j + 1) for graph in graphs[1:]])
    print(result)
0