Cod sursa(job #1908888)

Utilizator lflorin29Florin Laiu lflorin29 Data 7 martie 2017 10:49:30
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

const int nMax = 102;

const int INF = 1e8;

int n, cost[nMax][nMax], dp[nMax][nMax];

int main() {
	freopen("royfloyd.in", "r", stdin);
	freopen("royfloyd.out", "w", stdout);

	scanf("%d", &n);

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			scanf("%d", &cost[i][j]);

			if (!cost[i][j])
				cost[i][j] = INF;

			dp[i][j] = cost[i][j];
		}
	}

	for (int k = 1; k <= n; ++k) {
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j) {
				if (i != j and i != k and j != k) {
					dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
				}
			}
	}

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (dp[i][j] == INF)
				printf("%d ", 0);
			else printf("%d ", dp[i][j]);
		}

		printf("\n");
	}

	return 0;

}