Cod sursa(job #1365122)

Utilizator theprdvtheprdv theprdv Data 28 februarie 2015 04:20:39
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <vector>
#include <fstream>
using namespace std;
fstream fin("royfloyd.in", ios::in);
fstream fout("royfloyd.out", ios::out);

int n;
vector <int> graph[105];

void read()
{
	int x;
	fin >> n;
	for (int i = 0; i < n; i++)
		for (int j = 1; j <= n; j++)
			fin >> x,
			graph[i].push_back(x);
}
void royfloyd()
{
	for (int k = 0; k < n; k++)
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (graph[i][k] && graph[k][j] && (i!=j && !graph[i][j] || graph[i][j] > graph[i][k] + graph[k][j]))
					graph[i][j] = graph[i][k] + graph[k][j];
}

int main()
{
	read();
	royfloyd();
	for (int i = 0; i < n; i++, fout << endl)
		for (int j = 0; j < n; j++)
			fout << graph[i][j] << " ";

	fin.close();
	fout.close();
	return 0;
}