Cod sursa(job #2858485)

Utilizator robberttPopa Robert robbertt Data 27 februarie 2022 17:40:01
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ifstream cin("royfloyd.in");
    ofstream cout("royfloyd.out");
    int n;
    int mat[101][101];
    cin >> n;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> mat[i][j];
        }
    }
    for(int k = 0; k < n; ++k)
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j){
                if (mat[i][k] && mat[k][j] && (mat[i][j] > mat[i][k] + mat[k][j] || !mat[i][j]) && i != j)
                    mat[i][j] = mat[i][k] + mat[k][j];
            }
    //cout << '\n';
    for(int i = 0; i < n; ++i, cout << '\n')
        for(int j = 0; j < n; ++j){
            cout << mat[i][j] << ' ';
        }
    return 0;
}