zawatins-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/zawatins-library

:heavy_check_mark: wf (ワーシャルフロイド simple ver)
(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)

Verified with

Code

#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
Back to top page