Cod sursa(job #2080679)

Utilizator IulianBobocBoboc Iulian IulianBoboc Data 3 decembrie 2017 13:55:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include<iostream>
#include<fstream>
using namespace std;
#define maxConst 100
int weight[maxConst][maxConst];

int main() {
	ifstream inFile("royfloyd.in");
	ofstream outFile("royfloyd.out");

	int n, i, j, k;
	inFile >> n;
	for (i = 0; i < n; ++i) {
		for (j = 0; j < n; ++j) {
			inFile >> weight[i][j];
		}
	}


	for (k = 0; k < n; ++k) {
		for (i = 0; i < n; ++i) {
			for (j = 0; j < n; ++j) {
				if (i != j && weight[i][k] != 0 && weight[k][j] != 0 && (weight[i][j] > weight[i][k] + weight[k][j] || weight[i][j] == 0)) {
					weight[i][j] = weight[i][k] + weight[k][j];
				}
			}
		}
	}

	for (i = 0; i < n; ++i) {
		for (j = 0; j < n; ++j) {
			outFile << weight[i][j] << " ";
		}
		outFile << "\n";
	}

	inFile.close();
	outFile.close();
	return 0;
}