Cod sursa(job #2129303)

Utilizator AlexnolifeAlexandru Ica Alexnolife Data 12 februarie 2018 18:34:36
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>

std::ifstream f("royfloyd.in");
std::ofstream g("royfloyd.out");

int cost[101][101];
int N;

int main()
{
	f >> N;

	int i, j, k;

	for (i = 1; i <= N; i++) {
		for (j = 1; j <= N; j++) {
			f >> cost[i][j];
		}
	}
	
	for (k = 1; k <= N; ++k) {
		for (i = 1; i <= N; ++i) {
			for (j = 1; j <= N; ++j) {
				if (cost[i][k] && cost[i][j] && i != j && (cost[i][j] > cost[i][k] + cost[k][j] || !cost[i][j])) {
					cost[i][j] = cost[i][k] + cost[k][j];
				}
			}
		}
	}

	for (i = 1; i <= N; ++i, g << "\n") {
		for (j = 1; j <= N; ++j) {
			g << cost[i][j] << " ";
		}
	}

	return EXIT_SUCCESS;
}