Cod sursa(job #3039447)

Utilizator lolismekAlex Jerpelea lolismek Data 28 martie 2023 16:20:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>

using namespace std;

string filename = "royfloyd";

#ifdef LOCAL
    ifstream fin("input.in");
    ofstream fout("output.out");
#else
    ifstream fin(filename + ".in");
    ofstream fout(filename + ".out");
#endif

const int NMAX = 100;
const int INF = 1e9;

int rf[NMAX + 1][NMAX + 1];

signed main(){

    int n;
    fin >> n;

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

            if(rf[i][j] == 0 && i != j){
                rf[i][j] = INF;
            }
        }
    }

    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                rf[i][j] = min(rf[i][j], rf[i][k] + rf[k][j]);
            }
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(rf[i][j] == INF){
                fout << 0 << ' ';
            }else{
                fout << rf[i][j] << ' ';
            }
        }
        fout << '\n';
    }
    
    return 0;
}