Cod sursa(job #2351943)

Utilizator HumikoPostu Alexandru Humiko Data 22 februarie 2019 20:37:57
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include "pch.h"
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

int v[105][105];

int main()
{
	ios::sync_with_stdio(false);
	fin.tie(0); fout.tie(0);

	int n;
	fin >> n;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			fin >> v[i][j];
		}
	}

	for (int k = 1; k <= n; ++k) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				if (v[i][k] && v[k][j] && i != j) {
					v[i][j] = min(v[i][j] ? v[i][j] : (int)1e9, v[i][k] + v[k][j]);
				}
			}
		}
	}

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			fout << v[i][j] << " ";
		}
		fout << '\n';
	}
}