Cod sursa(job #2289391)

Utilizator StefanZamfirStefan Zamfir StefanZamfir Data 24 noiembrie 2018 15:27:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>

using namespace std;

int weight[100][100];

int main() {
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            cin >> weight[i][j];
            if(!weight[i][j]&&i!=j)
                weight[i][j] = 1001;
        }
    }
    for(int k=0;k<n;++k){
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                weight[i][j] = min(weight[k][j] + weight[i][k],weight[i][j]);
            }
        }
    }

    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            if(weight[i][j]==1001)
                weight[i][j]=0;
        }
    }

    for(int i=0;i<n;++i,cout << '\n'){
        for(int j=0;j<n;++j){
            cout << weight[i][j] << ' ';
        }
    }
}