Cod sursa(job #443121)

Utilizator tomescu_alinTomescu Alin tomescu_alin Data 16 aprilie 2010 04:43:24
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>

using namespace std;

typedef unsigned long ulong;

int main(int argc, char * argv[]) 
{
	
	const char * inFile = "royfloyd.in";
	const char * outFile = "royfloyd.out";
	
	ifstream fin(inFile);
	ofstream fout(outFile);
	
	#ifndef NDEBUG
		if(!fin || !fout)
		{
			cerr << "Error opening one of \"" << inFile << "\" or \"" << outFile << "\"" << endl;
			return -1;
		}
	#endif
	
	/**
	 *	Read the data in.
	 */
	ulong numVertices;
	fin >> numVertices;
	
	unsigned long costMatrix[numVertices + 1][numVertices + 1];
	
	ulong x, y;
	long cost;
	
	for(ulong i = 1; i <= numVertices; i++)
	{
		for(ulong j = 1; j <= numVertices; j++)
		{
			fin >> costMatrix[i][j];
		}
	}
	
	/**
	 *	Solve the problem.
	 */
	for(ulong k = 1; k <= numVertices; k++)
	{
		for(ulong i = 1; i <= numVertices; i++)
		{
			for(ulong j = 1; j <= numVertices; j++)
			{
				if(costMatrix[i][k] != 0 && costMatrix[k][j] != 0)
					costMatrix[i][j] = min(costMatrix[i][j], costMatrix[i][k] + costMatrix[k][j]);
			}
		}
	}
	
	for(ulong i = 1; i <= numVertices; i++)
	{
		for(ulong j = 1; j <= numVertices; j++)
		{
			fout << costMatrix[i][j] << " ";
		}
		
		fout << "\n";
	}
	
	/**
	 *	Cleanup!
	 */
	fout.close();
	fin.close();
	
	return 0;
}