Cod sursa(job #1589731)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 4 februarie 2016 12:54:06
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

#define NMax 110
#define INF 1000000000

using namespace std;

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

int nodes, dist[NMax][NMax];

int getMin(int a, int b)
{
	if (a < b)
		return a;
	return b;
}

int main()
{
	f >> nodes;
	for (int i = 1; i <= nodes; i++) {
		for (int j = 1; j <= nodes; j++) {
			f >> dist[i][j];

			if (!dist[i][j])
				dist[i][j] = INF;
		}
	}

	for (int k = 1; k <= nodes; k++) {
		for (int i = 1; i <= nodes; i++) {
			for (int j = 1; j <= nodes; j++) {
				if (i == j)
					continue;

				dist[i][j] = getMin(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}

	for (int i = 1; i <= nodes; i++) {
		for (int j = 1; j <= nodes; j++)
			if (dist[i][j] != INF)
				g << dist[i][j] << " ";
			else
				g << 0 << "\n";
		g << "\n";
	}
}