Cod sursa(job #2243588)

Utilizator eduardandrei20Nechifor Eduard Andrei eduardandrei20 Data 20 septembrie 2018 21:47:43
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
#define oo 1000000000
using namespace std;

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

int a[101][101], n;

int main() {
    in >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            in >> a[i][j];
        }
    }
     for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!a[i][j]){
                a[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 (a[i][j] > a[i][k] + a[k][j]){
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
     }
     for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[i][j] == oo || (i == j) ){
                out << "0 ";
            } else out << a[i][j] << " ";
        }
        out << "\n";
     }

    return 0;
}