Cod sursa(job #3130344)

Utilizator andrei.nita271@gmail.comAndrei Nita [email protected] Data 17 mai 2023 16:20:17
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int d[1000][1000];
const double INF = 1e9;
int main()
{
	int n;
	in >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			in >> d[i][j];
			if (d[i][j] == 0 && i != j) {
				d[i][j] = INF;
			}
		}
	}
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (d[i][k] + d[k][j] < d[i][j]) {
					d[i][j] = d[i][k] + d[k][j];
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			out << d[i][j] << ' ';
		}
		out << endl;
	}
}