Cod sursa(job #2718069)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 8 martie 2021 14:01:35
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

const int NMAX = 105;
const int INF = 2000009;
int n;
int a[NMAX][NMAX], dist[NMAX][NMAX];

void read()
{
	fin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dist[i][j] = 0;
	for(int i = 1 ; i <= n ; i++)
		for (int j = 1; j <= n; j++) {
			fin >> a[i][j];
			if (a[i][j] == 0)
				dist[i][j] = INF;
			else
				dist[i][j] = a[i][j];
		}
}

void solve()
{
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
			{
				if(i != j)
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
}

void write()
{
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			fout << (dist[i][j] == INF ? 0 : dist[i][j]) << " ";
		fout << "\n";
	}
}

int main()
{
	read();
	solve();
	write();
	return 0;
}