Cod sursa(job #3348085)

Utilizator nverde1119Popa Narcis Constantin nverde1119 Data 19 martie 2026 16:54:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;
int n, A[101][101];
const int inf = 9999999;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
void citire()
{
	f >> n;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			f >> A[i][j];
			if(i!=j && A[i][j] == 0)
				A[i][j] = inf;
		}
	}
}
void rf()
{
	int cost;
	for(int k = 1; k <= n; k++)
	{
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				if(A[i][k] != inf && A[k][j] != inf)
				{
					cost = A[i][k] + A[k][j];
					if(A[i][j] > cost)
						A[i][j] = cost;
				}
			}
		}
	}
}
void afis()
{
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			if(A[i][j] != inf)
				g << A[i][j] << ' ';
			else
				g << "0 ";
		}
		g << '\n';
	}
}
int main()
{
	citire();
	rf();
	afis();
	return 0;
}