ワーシャルフロイド法
lib/graph/floyd_warshall.hpp
インクルード
#include "graph/floyd_warshall.hpp"
関数
| 名前 | 説明 |
|---|---|
floyd_warshall |
ワーシャルフロイド法 |
検証
依存するヘッダ(1)
-
隣接行列
lib/graph/matrix_graph.hpp
検証プログラム(2)
-
test/aoj/grl/floyd_warshall.test.cpp
test/aoj/grl/floyd_warshall.test.cpp -
test/aoj/jag/bipartite_matching.test.cpp
test/aoj/jag/bipartite_matching.test.cpp
実装
#pragma once
#include <algorithm>
#include "graph/matrix_graph.hpp"
/// @brief ワーシャルフロイド法
/// @complexity 頂点数を $V$ として $O(V^3)$
template <class T>
void floyd_warshall(matrix_graph<T> &g) {
int n = g.size();
for (int i = 0; i < n; ++i) g[i][i] = T();
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) { g[i][j] = std::min(g[i][j], g[i][k] + g[k][j]); }
}
}
}