結果

問題 No.807 umg tours
ユーザー ayaoniayaoni
提出日時 2020-11-22 10:51:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,515 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 352,120 KB
最終ジャッジ日時 2023-09-30 22:35:19
合計ジャッジ時間 44,677 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 121 ms
76,956 KB
testcase_07 WA -
testcase_08 AC 100 ms
71,532 KB
testcase_09 AC 101 ms
71,584 KB
testcase_10 AC 103 ms
72,256 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1,856 ms
290,344 KB
testcase_25 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,collections,heapq
def MI(): return map(int,sys.stdin.readline().rstrip().split())


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

    def add(self, u, v, d, directed=False):
        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):  # sから各頂点までの最短距離
        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 = MI()
Di = Dijkstra()
for _ in range(M):
    a,b,c = MI()
    Di.add((a,0),(b,0),c,directed=False)
    Di.add((a,1),(b,1),c,directed=False)
    Di.add((a,0),(b,1),0,directed=True)
    Di.add((b,0),(a,1),c,directed=True)

dist = Di.search((1,0))
for i in range(1,N+1):
    print(dist[(i,0)]+dist[(i,1)] if i != 1 else 0)
0