Cod sursa(job #2368617)

Utilizator puzzleFlutur Vasile puzzle Data 5 martie 2019 16:46:46
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>

std::ifstream in("royfloyd.in");

const int Nmax = 101;

void EmptyDArray(int connections[][Nmax], int n)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			connections[i][j] = 0;
}

void Citire(int connections[][Nmax], int n)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			in >> connections[i][j];
	in.close();
}

void RoyFloyd(int connections[][Nmax], int n)
{
	for(int k = 0; k < n; k++)
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if (connections[i][k] &&
					connections[k][j] &&
					(connections[i][k] + connections[k][j] < connections[i][j] || !connections[i][j])
					&& i != j)
				{
					connections[i][j] = connections[i][k] + connections[k][j];
				}

}

void Print(int connections[][Nmax], int n)
{
	std::ofstream out("royfloyd.out");
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			out << connections[i][j] << " ";
		out << '\n';
	}

}

int main()
{
	int connections[Nmax][Nmax];
	int n;
	in >> n;
	EmptyDArray(connections, n);
	Citire(connections, n);
	RoyFloyd(connections,n);
	Print(connections, n);
}