Cod sursa(job #2454423)

Utilizator mvcl3Marian Iacob mvcl3 Data 8 septembrie 2019 13:59:50
Problema Floyd-Warshall/Roy-Floyd Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>

const std::string inFileName =	"royfloyd.in";
const std::string outFileName = "royfloyd.out";
const int MAX_WEIGHT_VALUE = 1001 * 1001;

inline void readData(std::ifstream& in, int& n, std::vector<std::vector<int>>& rf, std::vector<std::vector<bool>>& existVertice) {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			in >> rf[i][j];
			if (rf[i][j]) {
				existVertice[i][j] = true;
			}
			else {
				rf[i][j] = MAX_WEIGHT_VALUE;
			}
		}
	}
}

inline void solve(const int& n, std::vector<std::vector<int>>& rf, std::vector<std::vector<bool>>& existVertice) {
	for (int k = 0; k < n; ++k) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i != j && i != k && j != k && existVertice[i][k] && existVertice[k][j]) {
					rf[i][j] = std::min(rf[i][j], rf[i][k] + rf[k][j]);
				}
			}
		}
	}
}

int main()
{
	std::ifstream in(inFileName);
	std::ofstream out(outFileName);
	
	int n;
	in >> n;
	std::vector<std::vector<int>> rf(n, std::vector<int>(n));
	std::vector<std::vector<bool>> existVertice(n, std::vector<bool>(n));
	readData(in, n, rf, existVertice);
	solve(n, rf, existVertice);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			out << (rf[i][j] != MAX_WEIGHT_VALUE ? rf[i][j] : 0) << ' ';
		}
		out << std::endl;
	}

	return 0;
}