Cod sursa(job #3326570)

Utilizator radu._.21Radu Pelea radu._.21 Data 29 noiembrie 2025 14:16:02
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#define ll long long

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

int n,m,cost[101][101];

int main(){
    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++){
        // k e intermediarul
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(cost[i][j] > cost[i][k] + cost[k][j] || cost[i][j] == 0){
                    if(i!=k && j!=k && i!=j){
                        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;
}