Cod sursa(job #2732112)

Utilizator As932Stanciu Andreea As932 Data 28 martie 2021 18:52:12
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

using namespace std;

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

const int nMax = 1e3 + 5;

int n, a[nMax][nMax];

void read(){
    cin >> n;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin >> a[i][j];
}

void solve(){
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(i != j && a[i][k] && a[k][j])
                    if(a[i][j] > a[i][k] + a[k][j] || a[i][j] == 0)
                        a[i][j] = a[i][k] + a[k][j];
}

void print(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
}

int main()
{
    read();
    solve();
    print();

    return 0;
}