Cod sursa(job #2665784)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 31 octombrie 2020 12:15:52
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 f("royfloyd.in");
ofstream g("royfloyd.out");

int n,mat[105][105];

int main()
{
	f >> n;
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++) {
			f >> mat[i][j];
		}
	}
    for (int k=1;k<=n;k++) {
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=n;j++) {
				if (mat[i][k] != 0 && mat[k][j] != 0) {
					if (mat[i][j] == 0 && i != j) {
						mat[i][j] = mat[i][k] + mat[k][j];
					}
					else 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++) {
		for (int j=1;j<=n;j++) {
			g << mat[i][j] << " ";
		}
		g << '\n';
	}
    return 0;
}