提出 #22189616


ソースコード 拡げる

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)]
ME = [float('inf')]*N #自身から自身へ直接向かうコスト
graph = Dijkstra()
for a,b,c in ABC:
    if a==b:
        ME[a-1] = min(ME[a-1],c)
    else:
        graph.add(a-1,b-1,c,True)
#それぞれの街のダイクストラの結果を保持
CityD = []
for i in range(N):
    d,_ = graph.Dijkstra_search(i)
    CityD.append(d)
for i in range(N):
    #ansの初期値を自身から自身へ直接向かうコストとする
    #ちなみに、ダイクストラ内のアルゴリズムを少し編集することで、
    #自身に戻る距離を別で計算しなくてもよくなる方法もあります。
    #https://atcoder.jp/contests/abc191/submissions/22189422
    ans = ME[i]
    for j in range(N):
        if i!=j:#自身の距離は0のため、見ない
            ans = min(ans,CityD[i][j]+CityD[j][i])
    if ans == float('inf'):
        print(-1)
    else:
        print(ans)


提出情報

提出日時
問題 E - Come Back Quickly
ユーザ H20
言語 PyPy3 (7.3.0)
得点 500
コード長 2887 Byte
結果 AC
実行時間 2198 ms
メモリ 339288 KiB

ジャッジ結果

セット名 Sample All after_contest
得点 / 配点 0 / 0 500 / 500 0 / 0
結果
AC × 3
AC × 41
AC × 2
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All almost_path_00.txt, almost_path_01.txt, almost_path_02.txt, almost_path_03.txt, almost_path_04.txt, almost_path_05.txt, almost_path_06.txt, almost_path_07.txt, almost_path_08.txt, almost_path_09.txt, handmade_00.txt, handmade_01.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, sample_01.txt, sample_02.txt, sample_03.txt, wide_00.txt
after_contest after_contest_00.txt, after_contest_01.txt
ケース名 結果 実行時間 メモリ
after_contest_00.txt AC 1902 ms 229664 KiB
after_contest_01.txt AC 1619 ms 229556 KiB
almost_path_00.txt AC 2045 ms 339172 KiB
almost_path_01.txt AC 2175 ms 336888 KiB
almost_path_02.txt AC 2105 ms 337428 KiB
almost_path_03.txt AC 2198 ms 337984 KiB
almost_path_04.txt AC 2096 ms 339288 KiB
almost_path_05.txt AC 164 ms 76192 KiB
almost_path_06.txt AC 159 ms 77088 KiB
almost_path_07.txt AC 952 ms 127420 KiB
almost_path_08.txt AC 885 ms 126332 KiB
almost_path_09.txt AC 2037 ms 204128 KiB
handmade_00.txt AC 71 ms 65552 KiB
handmade_01.txt AC 58 ms 65348 KiB
random_00.txt AC 1975 ms 333496 KiB
random_01.txt AC 1845 ms 333796 KiB
random_02.txt AC 1888 ms 334224 KiB
random_03.txt AC 1899 ms 329448 KiB
random_04.txt AC 1879 ms 330248 KiB
random_05.txt AC 1723 ms 329204 KiB
random_06.txt AC 1764 ms 335464 KiB
random_07.txt AC 1696 ms 328028 KiB
random_08.txt AC 1659 ms 326124 KiB
random_09.txt AC 1628 ms 321736 KiB
random_10.txt AC 93 ms 74292 KiB
random_11.txt AC 101 ms 74784 KiB
random_12.txt AC 129 ms 76100 KiB
random_13.txt AC 120 ms 75904 KiB
random_14.txt AC 132 ms 76148 KiB
random_15.txt AC 513 ms 154072 KiB
random_16.txt AC 1094 ms 259704 KiB
random_17.txt AC 951 ms 241452 KiB
random_18.txt AC 367 ms 133920 KiB
random_19.txt AC 446 ms 102552 KiB
random_20.txt AC 983 ms 239532 KiB
random_21.txt AC 783 ms 237344 KiB
random_22.txt AC 495 ms 95496 KiB
random_23.txt AC 431 ms 90900 KiB
random_24.txt AC 910 ms 170436 KiB
sample_01.txt AC 69 ms 65624 KiB
sample_02.txt AC 59 ms 65780 KiB
sample_03.txt AC 62 ms 65484 KiB
wide_00.txt AC 1307 ms 148568 KiB