Cod sursa(job #2858484)

Utilizator robberttPopa Robert robbertt Data 27 februarie 2022 17:38:05
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 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][j] + mat[j][k] < mat[i][k]) && mat[i][j] != 0 && mat[j][k] != 0)
                    mat[i][k] = mat[i][j] + mat[j][k];
                else if(mat[i][k] == 0 && mat[i][j] != 0 && mat[j][k] != 0 && i != k)
                    mat[i][k] = mat[i][j] + mat[j][k];
            }
    //cout << '\n';
    for(int i = 0; i < n; ++i, cout << '\n')
        for(int j = 0; j < n; ++j){
            cout << mat[i][j] << ' ';
        }
    return 0;
}