結果

問題 No.807 umg tours
ユーザー 👑 H20H20
提出日時 2021-05-02 14:36:13
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 2,314 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 378,996 KB
最終ジャッジ日時 2023-09-28 04:59:34
合計ジャッジ時間 47,631 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 120 ms
77,420 KB
testcase_07 WA -
testcase_08 AC 90 ms
71,388 KB
testcase_09 AC 97 ms
72,496 KB
testcase_10 AC 97 ms
76,288 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1,635 ms
352,240 KB
testcase_25 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import heapq


class Dijkstra():
    def __init__(self):
        self.e = collections.defaultdict(list)

    def add(self, u, v, d, directed=False):
        """
        #0-indexedでなくてもよいことに注意
        #u = from, v = to, d = cost
        #directed = Trueなら、有向グラフである
        """
        if directed is False:
            self.e[u].append([v, d])
            self.e[v].append([u, d])
        else:
            self.e[u].append([v, d])

    def delete(self, u, v):
        self.e[u] = [_ for _ in self.e[u] if _[0] != v]
        self.e[v] = [_ for _ in self.e[v] if _[0] != u]

    def Dijkstra_search(self, s):
        """
        #0-indexedでなくてもよいことに注意
        #:param s: 始点
        #:return: 始点から各点までの最短経路と最短経路を求めるのに必要なprev
        """
        d = collections.defaultdict(lambda: float('inf'))
        prev = collections.defaultdict(lambda: None)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv, ud in self.e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    prev[uv] = u
                    heapq.heappush(q, (vd, uv))

        return d, prev

    def getDijkstraShortestPath(self, start, goal):
        _, prev = self.Dijkstra_search(start)
        shortestPath = []
        node = goal
        while node is not None:
            shortestPath.append(node)
            node = prev[node]
        return shortestPath[::-1]
N, M = map(int, input().split())
ABC = [list(map(int, input().split())) for i in range(M)]
graph1 = Dijkstra()#チケット使用しない
graph2 = Dijkstra()#チケット1枚使用(使用後をマイナスで表現
for a,b,c in ABC:
    graph1.add(a, b, c)
    graph2.add(a, b, c)
    graph2.add(a, -b, 0)
    graph2.add(-a, -b, c)
g1,_ = graph1.Dijkstra_search(1)
g2,_ = graph2.Dijkstra_search(1)
print(0)#1から1は0、↓の計算でやると別な値が入るため
for i in range(2,N+1):
    print(g1[i]+g2[-i])
0