Cod sursa(job #2444623)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 31 iulie 2019 20:43:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

int cost[105][105];

int main()
{
    ifstream fin("royfloyd.in");
    ofstream fout("royfloyd.out");
    int n;
    fin >> n;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            fin >> cost[i][j];
    }
    for(int k = 1; k <= n; ++k){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j)
                if(cost[i][k] && cost[k][j] && i != j){
                    if(cost[i][j]) cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
                    else cost[i][j] = cost[i][k] + cost[k][j];
                }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            fout << cost[i][j] << " ";
        fout << "\n";
    }
    return 0;
}