結果

問題 No.1344 Typical Shortest Path Sum
ユーザー lam6er
提出日時 2025-04-16 16:16:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,009 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 66,460 KB
最終ジャッジ日時 2025-04-16 16:18:41
合計ジャッジ時間 5,564 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1
    INF = 10**18
    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 = int(input[idx])
        idx += 1
        t = int(input[idx])
        idx += 1
        d = int(input[idx])
        idx += 1
        if dist[s][t] > d:
            dist[s][t] = d
    
    # Floyd-Warshall algorithm
    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 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)

if __name__ == '__main__':
    main()
0