Cod sursa(job #2145745)

Utilizator Tyler_BMNIon Robert Gabriel Tyler_BMN Data 27 februarie 2018 16:32:51
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>

using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

const int INF = 0x3f3f3f3f;
const int MAXN = 111;
int n;
int mat[MAXN][MAXN];

void royfloyd() {
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (mat[i][j] > mat[i][k] + mat[k][j]) {
					mat[i][j] = mat[i][k] + mat[k][j];
				}
			}
		}
	}
}


void input() {
	fin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			fin >> mat[i][j];
			if (mat[i][j] == 0 && i != j) {
				mat[i][j] = INF;
			}
		}
	}
}

void output() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			fout << mat[i][j] << " ";
		}
		fout << "\n";
	}
}
int main() {
	input();
	royfloyd();
	output();
}