Cod sursa(job #3231410)

Utilizator luc3lexaAlexandrescu Luca luc3lexa Data 26 mai 2024 12:29:09
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

const int nmax = 101;
const int inf = 0x3F3F3F3F;
int n,mat[nmax][nmax];
void read_input(){
	fin >> n;
	for(int i = 1; i <=n; i++){
		for(int j = 1; j <=n; j++){
		    fin >> mat[i][j];
		    if(mat[i][j] == 0 && i != j){
		       mat[i][j] = inf;
		    }
		}
	}
}
void solve(){
	for(int k = 1; k <=n; k++){
		for(int i = 1; i <=n; i++){
			for(int j = 1; j <=n; j++){
				if(mat[i][j] > mat[i][k] + mat[k][j]){
					mat[i][j] = mat[i][k] + mat[k][j];
				}
			}
		}
	};
	for(int i = 1; i <=n; i++,fout << '\n'){
		for(int j = 1; j <=n; j++){
			fout << mat[i][j] << " ";
		}
	}
} 
int main(){
	read_input();
	solve();
	return 0;
}