Cod sursa(job #1420567)

Utilizator cosgbCosmin cosgb Data 18 aprilie 2015 18:32:23
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>

int dist[100][100];

using namespace std;

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

	int n, sum;
	in >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			in >> dist[i][j];
		}
	}
	for (int k = 0; k < n; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j)
					continue;
				if (dist[i][k] && dist[k][j]) {
					sum = dist[i][k] + dist[k][j];
					if (!dist[i][j] || dist[i][j] > sum) {
						dist[i][j] = sum;
					}
				}
			}
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			out << dist[i][j] << " ";
		}
		out << "\n";
	}

	in.close();
	out.close();

	return 0;
}