提出 #22260844


ソースコード 拡げる

Copy
import itertools
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] = c
self.d[v][u] = c
else:
self.d[u][v] = c
def WarshallFloyd_search_result(self):
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import itertools

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] = c
            self.d[v][u] = c
        else:
            self.d[u][v] = c

    def WarshallFloyd_search_result(self):
        #不要であればFalseとする
        USE = [[True] * N for i in range(N)]

        for k in range(self.N):
            for i in range(self.N):
                for j in range(self.N):

                    #矛盾
                    if self.d[i][j] > self.d[i][k] + self.d[k][j]:
                        return -1 
                    #不要となるコストを引く
                    if self.d[i][j] == self.d[i][k] + self.d[k][j] and i!=k and k!=j:
                        USE[i][j]=False
        #総コスト
        ans = 0
        for i in range(self.N):
            for j in range(self.N):
                if USE[i][j]:
                    ans+=self.d[i][j]
        #行き来で2回カウントしているため、半分にして返却
        return ans//2

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
graph = WarshallFloyd(N)
for i in range(N):
    for j in range(N):
        graph.add(i, j, A[i][j], True)#Trueにする必要はないけれど気持ち的に
print(graph.WarshallFloyd_search_result())

提出情報

提出日時
問題 D - Restoring Road Network
ユーザ H20
言語 PyPy3 (7.3.0)
得点 500
コード長 1725 Byte
結果 AC
実行時間 759 ms
メモリ 71572 KB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 4
AC × 17
セット名 テストケース
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
ケース名 結果 実行時間 メモリ
01.txt AC 759 ms 71236 KB
02.txt AC 456 ms 71228 KB
03.txt AC 447 ms 71424 KB
04.txt AC 418 ms 71140 KB
05.txt AC 411 ms 71408 KB
06.txt AC 399 ms 71384 KB
07.txt AC 84 ms 69512 KB
08.txt AC 404 ms 71572 KB
09.txt AC 404 ms 71548 KB
10.txt AC 89 ms 69856 KB
11.txt AC 79 ms 69684 KB
12.txt AC 84 ms 69440 KB
13.txt AC 49 ms 62120 KB
subtask0_0.txt AC 54 ms 62344 KB
subtask0_1.txt AC 54 ms 62276 KB
subtask0_2.txt AC 51 ms 62096 KB
subtask0_3.txt AC 51 ms 62036 KB


2025-03-31 (月)
09:17:47 +00:00