結果

問題 No.807 umg tours
ユーザー 👑 H20H20
提出日時 2021-05-02 14:50:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,107 ms / 4,000 ms
コード長 1,747 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 293,980 KB
最終ジャッジ日時 2023-09-28 05:14:25
合計ジャッジ時間 23,486 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
76,976 KB
testcase_01 AC 113 ms
77,452 KB
testcase_02 AC 107 ms
77,568 KB
testcase_03 AC 123 ms
77,732 KB
testcase_04 AC 94 ms
76,612 KB
testcase_05 AC 97 ms
77,260 KB
testcase_06 AC 105 ms
77,792 KB
testcase_07 AC 104 ms
77,724 KB
testcase_08 AC 82 ms
71,684 KB
testcase_09 AC 86 ms
72,460 KB
testcase_10 AC 85 ms
72,356 KB
testcase_11 AC 1,282 ms
229,640 KB
testcase_12 AC 1,092 ms
213,308 KB
testcase_13 AC 1,485 ms
255,500 KB
testcase_14 AC 762 ms
156,440 KB
testcase_15 AC 608 ms
144,704 KB
testcase_16 AC 1,716 ms
269,560 KB
testcase_17 AC 2,107 ms
293,980 KB
testcase_18 AC 2,064 ms
293,100 KB
testcase_19 AC 1,971 ms
292,760 KB
testcase_20 AC 1,005 ms
223,368 KB
testcase_21 AC 1,014 ms
222,888 KB
testcase_22 AC 518 ms
135,996 KB
testcase_23 AC 445 ms
124,708 KB
testcase_24 AC 943 ms
260,020 KB
testcase_25 AC 2,073 ms
290,884 KB
権限があれば一括ダウンロードができます

ソースコード

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 search(self, s):
        """
        :param s: 始点
        :return: 始点から各点までの最短経路
        """
        d = collections.defaultdict(lambda: float('inf'))
        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
                    heapq.heappush(q, (vd, uv))

        return d
N, M = map(int, input().split())
ABC = [list(map(int, input().split())) for i in range(M)]
graph = Dijkstra()#チケット使用しない(使用後をマイナスで表現)
for a,b,c in ABC:
    graph.add(a, b, c)
    graph.add(a, -b, 0, True)
    graph.add(b, -a, 0, True)
    graph.add(-a, -b, c)
g = graph.search(1)
print(0)#1から1は0、↓の計算でやると別な値が入るため
for i in range(2,N+1):
    print(g[i]+g[-i])
0