Cod sursa(job #3235646)

Utilizator addanciuAdriana Danciu addanciu Data 19 iunie 2024 16:35:26
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <vector>
#include <algorithm>
#include <queue>
#include <fstream>

using namespace std;

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

const int N = 100;
const int INF = 1e6;

int d[N + 1][N + 1], n;

int main() {
    in >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            in >> d[i][j];
            if(i != j && d[i][j] == 0){
                d[i][j] = INF;
            }
        }
    }
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(d[i][k] + d[k][j] < d[i][j]){
                    d[i][j] = d[i][k] + d[k][j];
                }
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(d[i][j] == INF){
                d[i][j] = 0;
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            out << d[i][j] << " ";
        }
        out << '\n';
    }
    return 0;
}