結果
問題 |
No.1344 Typical Shortest Path Sum
|
ユーザー |
![]() |
提出日時 | 2021-01-16 13:44:31 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 201 ms / 2,000 ms |
コード長 | 800 bytes |
コンパイル時間 | 254 ms |
コンパイル使用メモリ | 12,416 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-11-27 14:25:25 |
合計ジャッジ時間 | 5,003 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 77 |
ソースコード
#yuki-1344 import heapq N, M = map(int, input().split()) Edge = set() for _ in range(M): s, t, d = map(int, input().split()) s -= 1 t -= 1 Edge.add((s, t, d)) # O(EV) def bellman_ford(s): path = [float('inf')]*N # 各頂点への最小コスト path[s] = 0 # 自身への距離は0 for i in range(N): update = False # 更新が行われたか for s, t, d in Edge: if path[t] > path[s] + d: path[t] = path[s] + d update = True if not update: break # 負閉路が存在 if i == N - 1: return -1 return path for i in range(N): path = bellman_ford(i) for j in range(N): if path[j] == float('inf'): path[j] = 0 print(sum(path))