mmrz's library

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


Project maintained by mm-rz Hosted on GitHub Pages — Theme by mattgraham

:heavy_check_mark: 全頂点間最短距離 O(V^3) 及び更新を O(V^2) で行うアルゴリズム (ワーシャルフロイド法)
(graph/warshall_floyd.hpp)

全頂点間最短距離 O(V^3) 及び更新を O(V^2) で行うアルゴリズム (ワーシャルフロイド法)

使い方

Verified with

Code

#pragma once

#include<numeric>
#include<vector>

template<typename T>
struct warshall_floyd {
private:
	int V;
public:
	std::vector<std::vector<T>> dist;

	warshall_floyd(std::vector<std::vector<T>> &edge_cost, T infty=std::numeric_limits<T>::max()/2) : V(ssize(edge_cost)), dist(edge_cost){
		for(int k = 0;k < V;k++){
			for(int i = 0;i < V;i++){
				if(dist[i][k] == infty)continue;
				for(int j = 0;j < V;j++){
					if(dist[k][j] == infty)continue;
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
	}

	void update(int s, int t, T cost){
		dist[s][t] = cost;
		for(int u = 0;u < V;u++){
			for(int v = 0;v < V;v++){
				dist[u][v] = min(dist[u][v], dist[u][s]+dist[s][t]+dist[t][v]);
			}
		}
	}
};
#line 2 "graph/warshall_floyd.hpp"

#include<numeric>
#include<vector>

template<typename T>
struct warshall_floyd {
private:
	int V;
public:
	std::vector<std::vector<T>> dist;

	warshall_floyd(std::vector<std::vector<T>> &edge_cost, T infty=std::numeric_limits<T>::max()/2) : V(ssize(edge_cost)), dist(edge_cost){
		for(int k = 0;k < V;k++){
			for(int i = 0;i < V;i++){
				if(dist[i][k] == infty)continue;
				for(int j = 0;j < V;j++){
					if(dist[k][j] == infty)continue;
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
	}

	void update(int s, int t, T cost){
		dist[s][t] = cost;
		for(int u = 0;u < V;u++){
			for(int v = 0;v < V;v++){
				dist[u][v] = min(dist[u][v], dist[u][s]+dist[s][t]+dist[t][v]);
			}
		}
	}
};
Back to top page