Submission #22260844


Source Code Expand

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())

Submission Info

Submission Time
Task D - Restoring Road Network
User H20
Language PyPy3 (7.3.0)
Score 500
Code Size 1725 Byte
Status AC
Exec Time 759 ms
Memory 71572 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 17
Set Name Test Cases
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
Case Name Status Exec Time Memory
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-27 (Thu)
16:36:54 +00:00