結果

問題 No.1344 Typical Shortest Path Sum
ユーザー lam6er
提出日時 2025-03-31 17:33:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,041 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 67,200 KB
最終ジャッジ日時 2025-03-31 17:34:13
合計ジャッジ時間 5,471 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[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(data[idx])
        idx += 1
        t = int(data[idx])
        idx += 1
        d = int(data[idx])
        idx += 1
        if d < dist[s][t]:
            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 sums
    for i in range(1, N + 1):
        total = 0
        for j in range(1, N + 1):
            if i == j:
                continue
            if dist[i][j] < INF:
                total += dist[i][j]
        print(total)

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