Cod sursa(job #1456529)

Utilizator tony.hegyesAntonius Cezar Hegyes tony.hegyes Data 1 iulie 2015 08:43:49
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <climits>
using namespace std;

int main(int argc, char **argv)
{
	ifstream indata("royfloyd.in");
	
	// input data
	int n;
	indata >> n;
	int cost_mat[n][n];
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
				indata >> cost_mat[i][j];
				if  (cost_mat[i][j] == 0) {
						cost_mat[i][j] = INT_MAX;
				}
		}
	}
	
	// apply Roy-Floyd to find shortestPath
	for (int k = 0; k < n; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if(cost_mat[i][j] > cost_mat[i][k] + cost_mat[k][j])
					cost_mat[i][j] = cost_mat[i][k] + cost_mat[k][j];
			}
		}
	}
	
	// output data
	ofstream outdata("royfloyd.out");
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			outdata << cost_mat[i][j] << " ";
		}
		outdata << "\n";
	}
	
	indata.close();
	outdata.close();
	return 0;
}