Cod sursa(job #2692035)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 31 decembrie 2020 11:39:07
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int d[101][101];
int n, m;
int main()
{
	int x, y,z;
	fin >> n;
	for(int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			fin >> x;
			if (x == 0 && i != j)
				d[i][j] = 1e9;
			else d[i][j] = x;
		}

	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (d[i][k] + d[k][j] < d[i][j])
					d[i][j] = d[i][k] + d[k][j];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			if (d[i][j] == 1e9) fout << "0 ";
			else fout << d[i][j] << " ";
		fout << "\n";
	}

	fin.close();
	fout.close();
	return 0;
}