Cod sursa(job #1750820)

Utilizator dtoniucDaniel Toniuc dtoniuc Data 31 august 2016 09:28:20
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>

#define MAX_DIM 101
#define MAX_VAL 1001

using namespace std;

void ReadInput(int cost[][MAX_DIM], int* n);
void WriteResult(int cost[][MAX_DIM], int n);
void RoyFloyd(int cost[][MAX_DIM], int n);

int main()
{
	int cost[MAX_DIM][MAX_DIM];
	int n;

	ReadInput(cost, &n);

	RoyFloyd(cost, n);

	WriteResult(cost, n);

	return 0;
}

void ReadInput(int cost[][MAX_DIM], int* n)
{
	ifstream fin;
	fin.open("royfloyd.in");

	fin >> *n;

	for(int i = 0; i < *n; ++i)
		for(int j = 0; j < *n; ++j)
		{
			fin >> cost[i][j];
			if(cost[i][j] == 0 && i != j)
			{
				cost[i][j] = MAX_VAL;
			}
		}
	fin.close();
}

void RoyFloyd(int cost[][MAX_DIM], int n)
{
	for(int k = 0; k < n; k++)
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if(cost[i][j] > cost[i][k] + cost[k][j])
					cost[i][j] = cost[i][k] + cost[k][j];
}
void WriteResult(int cost[][MAX_DIM], int n)
{
	ofstream fout;
		fout.open("royfloyd.out");
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
				fout << cost[i][j] << " ";
			fout << "\n";
		}
}