Cod sursa(job #2427373)

Utilizator teoschipor00Teodora Schipor teoschipor00 Data 31 mai 2019 17:35:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
#define Nmax 101
using namespace std;

int mat[Nmax][Nmax];
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

int main()
{
    int n;
	f >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			f >> mat[i][j];

	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 0; j <= n; j++)
				if ((mat[i][j] == 0 or mat[i][j] > mat[i][k] + mat[k][j]) and mat[i][k] and mat[i][k] and mat[k][j] and i != j)
					mat[i][j] = mat[i][k] + mat[k][j];

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			g << mat[i][j] << " ";

		g << endl;
	}

	return 0;
}