結果

問題 No.1344 Typical Shortest Path Sum
ユーザー lam6er
提出日時 2025-03-20 21:14:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 760 bytes
コンパイル時間 138 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 73,816 KB
最終ジャッジ日時 2025-03-20 21:15:24
合計ジャッジ時間 5,741 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())

INF = 10**18

# Initialize distance matrix with 1-based indexing
dist = [[INF] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    dist[i][i] = 0

for _ in range(m):
    s, t, d = map(int, input().split())
    if dist[s][t] > d:
        dist[s][t] = d

# Floyd-Warshall algorithm to compute all pairs shortest paths
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            if dist[i][k] + dist[k][j] < dist[i][j]:
                dist[i][j] = dist[i][k] + dist[k][j]

# Calculate and print the sum for each node
for i in range(1, n + 1):
    total = 0
    for j in range(1, n + 1):
        if i != j and dist[i][j] < INF:
            total += dist[i][j]
    print(total)
0