Cod sursa(job #1969783)

Utilizator tudormaximTudor Maxim tudormaxim Data 18 aprilie 2017 17:28:19
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int maxn = 105;
const int oo = 1 << 30;
int G[maxn][maxn];
int n;

int main() {
    ios_base :: sync_with_stdio(false);
    fin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> G[i][j];
            if (G[i][j] == 0 && i != j) G[i][j] = oo;
        }
    }
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (G[i][k] + G[k][j] < G[i][j]) G[i][j] = G[i][k] + G[k][j];
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (G[i][j] == oo) G[i][j] = 0;
            fout << G[i][j] << " ";
        }
        fout << "\n";
    }
    return 0;
}