結果

問題 No.1344 Typical Shortest Path Sum
ユーザー 👑 H20H20
提出日時 2021-05-01 17:19:05
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,940 bytes
コンパイル時間 2,829 ms
コンパイル使用メモリ 86,804 KB
実行使用メモリ 78,220 KB
最終ジャッジ日時 2023-09-27 07:11:05
合計ジャッジ時間 12,925 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,332 KB
testcase_01 AC 69 ms
71,244 KB
testcase_02 AC 70 ms
71,432 KB
testcase_03 AC 166 ms
76,500 KB
testcase_04 AC 78 ms
76,360 KB
testcase_05 AC 68 ms
71,388 KB
testcase_06 AC 68 ms
71,236 KB
testcase_07 AC 69 ms
71,388 KB
testcase_08 AC 69 ms
71,192 KB
testcase_09 AC 71 ms
71,384 KB
testcase_10 AC 70 ms
71,180 KB
testcase_11 AC 70 ms
71,228 KB
testcase_12 AC 69 ms
71,104 KB
testcase_13 AC 67 ms
71,188 KB
testcase_14 AC 69 ms
71,180 KB
testcase_15 AC 66 ms
71,312 KB
testcase_16 AC 106 ms
77,504 KB
testcase_17 AC 69 ms
71,412 KB
testcase_18 AC 68 ms
71,236 KB
testcase_19 AC 69 ms
71,188 KB
testcase_20 AC 69 ms
71,408 KB
testcase_21 AC 68 ms
71,440 KB
testcase_22 AC 69 ms
71,308 KB
testcase_23 AC 70 ms
71,256 KB
testcase_24 AC 67 ms
71,340 KB
testcase_25 AC 68 ms
71,184 KB
testcase_26 AC 69 ms
71,308 KB
testcase_27 AC 68 ms
71,124 KB
testcase_28 AC 69 ms
71,192 KB
testcase_29 AC 70 ms
71,464 KB
testcase_30 AC 68 ms
71,340 KB
testcase_31 AC 65 ms
71,344 KB
testcase_32 AC 78 ms
71,292 KB
testcase_33 AC 69 ms
71,040 KB
testcase_34 AC 69 ms
71,348 KB
testcase_35 AC 68 ms
71,396 KB
testcase_36 AC 69 ms
71,352 KB
testcase_37 AC 69 ms
71,220 KB
testcase_38 AC 69 ms
71,088 KB
testcase_39 AC 68 ms
71,180 KB
testcase_40 AC 129 ms
77,432 KB
testcase_41 AC 136 ms
77,540 KB
testcase_42 AC 125 ms
77,796 KB
testcase_43 AC 123 ms
77,724 KB
testcase_44 AC 128 ms
77,564 KB
testcase_45 AC 131 ms
77,584 KB
testcase_46 AC 130 ms
77,792 KB
testcase_47 AC 127 ms
77,620 KB
testcase_48 AC 131 ms
77,720 KB
testcase_49 AC 120 ms
77,948 KB
testcase_50 AC 125 ms
77,564 KB
testcase_51 AC 130 ms
77,812 KB
testcase_52 AC 124 ms
77,452 KB
testcase_53 AC 135 ms
77,756 KB
testcase_54 AC 124 ms
77,440 KB
testcase_55 AC 129 ms
77,744 KB
testcase_56 AC 126 ms
77,696 KB
testcase_57 AC 128 ms
77,700 KB
testcase_58 AC 147 ms
77,944 KB
testcase_59 AC 126 ms
77,548 KB
testcase_60 AC 78 ms
75,796 KB
testcase_61 AC 69 ms
71,236 KB
testcase_62 AC 110 ms
78,120 KB
testcase_63 AC 83 ms
76,516 KB
testcase_64 AC 86 ms
76,332 KB
testcase_65 AC 115 ms
78,132 KB
testcase_66 AC 112 ms
78,216 KB
testcase_67 AC 112 ms
78,220 KB
testcase_68 AC 92 ms
76,160 KB
testcase_69 AC 83 ms
76,404 KB
testcase_70 AC 83 ms
76,048 KB
testcase_71 AC 68 ms
71,320 KB
testcase_72 AC 70 ms
71,036 KB
testcase_73 AC 69 ms
71,036 KB
testcase_74 AC 70 ms
71,432 KB
testcase_75 AC 70 ms
71,420 KB
testcase_76 AC 71 ms
71,236 KB
testcase_77 AC 71 ms
71,188 KB
testcase_78 AC 69 ms
71,252 KB
testcase_79 AC 98 ms
77,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class WarshallFloyd():
    def __init__(self, N):
        self.N = N
        self.d = [[float("inf") for i in range(N)]
                  for i in range(N)]  # d[u][v] : 辺uvのコスト(存在しないときはinf)

    def add(self, u, v, c, directed=False):
        """
        0-indexedであることに注意
        u = from, v = to, c = cost
        directed = Trueなら、有向グラフである
        """
        #多重辺を持つ可能性がある
        #元のアルゴリズムは上書きとなっているが、これを更新に変更する必要がある
        if directed is False:
            self.d[u][v] = min(c,self.d[u][v])
            self.d[v][u] = min(c,self.d[v][u])
        else:
            self.d[u][v] = min(c,self.d[u][v])

    def WarshallFloyd_search(self):
        # これを d[i][j]: iからjへの最短距離 にする
        # 本来無向グラフでのみ全域木を考えるが、二重辺なら有向でも行けそう
        # d[i][i] < 0 なら、グラフは負のサイクルを持つ
        for k in range(self.N):
            for i in range(self.N):
                for j in range(self.N):
                    self.d[i][j] = min(
                        self.d[i][j], self.d[i][k] + self.d[k][j])
        hasNegativeCycle = False
        for i in range(self.N):
            if self.d[i][i] < 0:
                hasNegativeCycle = True
                break
        for i in range(self.N):
            self.d[i][i] = 0
        return hasNegativeCycle, self.d

N, M = map(int, input().split())
STD = [list(map(int, input().split())) for i in range(M)]
graph = WarshallFloyd(N)
for s,t,d in STD:
    graph.add(s-1, t-1, d, True)
hasNegativeCycle, d = graph.WarshallFloyd_search()#負経路はないため、使用するのはdの方のみ
for i in range(N):
    for j in range(N):
        if d[i][j] == float("inf"):
            d[i][j]=0
for i in range(N):
    print(sum(d[i]))
0