結果
| 問題 |
No.1344 Typical Shortest Path Sum
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-06-10 03:39:23 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,183 bytes |
| コンパイル時間 | 235 ms |
| コンパイル使用メモリ | 82,440 KB |
| 実行使用メモリ | 69,752 KB |
| 最終ジャッジ日時 | 2024-09-21 05:41:31 |
| 合計ジャッジ時間 | 7,838 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 77 |
ソースコード
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
import sys
from itertools import combinations, permutations, product, accumulate, groupby
from collections import defaultdict, deque, Counter
from functools import reduce
from operator import add, mul
import heapq
import bisect
import math
import copy
from sklearn.cluster import ward_tree
sys.setrecursionlimit(10 ** 9)
input = lambda: sys.stdin.readline().rstrip()
INF = float("inf")
MOD = 10 ** 9 + 7
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)
_, d = graph.WarshallFloyd_search()
for i in range(N):
for j in range(N):
if d[i][j] == INF:
d[i][j] = 0
for i in range(N):
print(sum(d[i]))