Cod sursa(job #3247506)

Utilizator Alexandru_cioAlexandru Ciobanica Alexandru_cio Data 8 octombrie 2024 09:15:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1e3;
const int INF = 1e9;
int c[NMAX + 1][NMAX + 1];

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

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