Cod sursa(job #3322494)

Utilizator GoreaRaresGorea Rares-Andrei GoreaRares Data 14 noiembrie 2025 14:41:50
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define inf 1e6

using namespace std;

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

int n, a[101][101], dist[101][101];

void read(){
    fin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++){
            fin >> a[i][j];
            if(i == j)
                dist[i][j] = 0;
            else
                if(a[i][j] == 0)
                    dist[i][j] = inf;
                else
                    dist[i][j] = 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(dist[i][k] != inf && dist[k][j] != inf)
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            fout << (dist[i][j] == inf ? 0 : dist[i][j]) << " ";
        fout << "\n";
    }
}

int main()
{
    read();
    solve();
    return 0;
}