Cod sursa(job #2194962)

Utilizator gabriel-mocioacaGabriel Mocioaca gabriel-mocioaca Data 14 aprilie 2018 19:12:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>

#define cin in
#define cout out

using namespace std;

int main() {
	ifstream in("royfloyd.in");
	ofstream out("royfloyd.out");
	
	int n;
	int graph[101][101];
	int i, j, k;

	cin >> n;

	for (i = 1; i <= n; ++i)
		for (j = 1; j <= n; ++j)
			cin >> graph[i][j];

	for (k = 1; k <= n; ++k)
		for (i = 1; i <= n; ++i)
			for (j = 1; j <= n; ++j)
				if (graph[i][k] && graph[k][j] && (graph[i][j] > graph[i][k] + graph[k][j] || !graph[i][j]) && i != j)
					graph[i][j] = graph[i][k] + graph[k][j];

	for (i = 1; i <= n; ++i) {
		for (j = 1; j <= n; ++j)
			cout << graph[i][j] << ' ';
		cout << '\n';
	}

	in.close();
	out.close();

	return 0;
}