This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/graph/simple/wf.hpp"
辺重み付きグラフ $G(V, E)$ の全対間最短経路を計算します。
std::vector<std::vector<cost_type>> wf<cost_type>(const std::vector<std::vector<std::pair<int, cost_type>>>& G, cost_type inf = std::numeric_limits<cost_type>::max() / 3)
cost_type
: 辺の重みの型(テンプレート引数)
int
、long
、long long
以外での使用を想定していません。G
: グラフの隣接リストを表す二次元vector
u
からv
へのコストw
の有向辺 $(u, v, w) \in E$ について、G[u]
にstd::pair(v, w)
が存在するようにしてくださいzawa::read_weighted_graph
を用いて標準入力から対応するG
を生成できます。inf
: テーブル更新の初期値
std::numeric_limits<cost_type>::max() / 3
が定められていますが、自分で指定することを強く推奨します。返り値
: 全対間最短経路を表す二次元vector
res[i][j]
に頂点i
から頂点j
への最短経路の総コストが入ります。i
から頂点j
に到達できない時に、res[i][j] == inf
とは限らないことに注意してください(チェックの仕方は例えばtest/simple-wf1.test.cpp
を確認してください)#pragma once
#include <vector>
#include <utility>
#include <limits>
#include <algorithm>
namespace zawa {
template <class cost_type>
std::vector<std::vector<cost_type>> wf(const std::vector<std::vector<std::pair<int, cost_type>>>& G, cost_type inf = std::numeric_limits<cost_type>::max() / 3) {
std::vector res(G.size(), std::vector(G.size(), inf));
for (std::size_t i = 0 ; i < G.size() ; i++) {
res[i][i] = 0;
}
for (std::size_t i = 0 ; i < G.size() ; i++) {
for (auto [x, w] : G[i]) {
res[i][x] = std::min(res[i][x], w);
}
}
for (std::size_t k = 0 ; k < G.size() ; k++) {
for (std::size_t i = 0 ; i < G.size() ; i++) {
for (std::size_t j = 0 ; j < G.size() ; j++) {
res[i][j] = std::min(res[i][j], res[i][k] + res[k][j]);
}
}
}
return res;
}
} // namespace zawa
#line 2 "src/graph/simple/wf.hpp"
#include <vector>
#include <utility>
#include <limits>
#include <algorithm>
namespace zawa {
template <class cost_type>
std::vector<std::vector<cost_type>> wf(const std::vector<std::vector<std::pair<int, cost_type>>>& G, cost_type inf = std::numeric_limits<cost_type>::max() / 3) {
std::vector res(G.size(), std::vector(G.size(), inf));
for (std::size_t i = 0 ; i < G.size() ; i++) {
res[i][i] = 0;
}
for (std::size_t i = 0 ; i < G.size() ; i++) {
for (auto [x, w] : G[i]) {
res[i][x] = std::min(res[i][x], w);
}
}
for (std::size_t k = 0 ; k < G.size() ; k++) {
for (std::size_t i = 0 ; i < G.size() ; i++) {
for (std::size_t j = 0 ; j < G.size() ; j++) {
res[i][j] = std::min(res[i][j], res[i][k] + res[k][j]);
}
}
}
return res;
}
} // namespace zawa