Cod sursa(job #3376840)

Utilizator AlphxByteStefan Alexandru AlphxByte Data 20 noiembrie 2026 08:40:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

const std::string fileName = "royfloyd";
const int N = 1e2 + 1;
const int INF = 0x3F3F3F3F;

std::ifstream fin(fileName + ".in");
std::ofstream fout(fileName + ".out");

int distance[N][N];

int main()
{
	int n;
	fin >> n;
	for(int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			fin >> distance[i][j];
			if (i != j && distance[i][j] == 0)
				distance[i][j] = INF;
		}

	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				if (distance[i][j] > distance[i][k] + distance[k][j])
					distance[i][j] = distance[i][k] + distance[k][j];

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			fout << (distance[i][j] != INF ? distance[i][j] : 0) << ' ';
		fout << '\n';
	}

	return 0;
}