Cod sursa(job #1750819)

Utilizator dtoniucDaniel Toniuc dtoniuc Data 31 august 2016 09:23:15
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>

#define MAX_DIM 101
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];

	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";
		}
}