Cod sursa(job #3325353)

Utilizator ninelcatelALEXANDRU-NICOLAS NEGRISAN ninelcatel Data 25 noiembrie 2025 12:54:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
const long long INF = INT_MAX;
// O(n^3)
std::ifstream fin("royfloyd.in");
std::ofstream fout("royfloyd.out");

int main(){
    int n;
    fin>>n;
    std::vector<std::vector<int>> dist;
    dist.resize(n+1);
    std::fill(dist.begin(),dist.end(),std::vector<int>(n+1));
    for(int i=1;i<=n;i++){
        for(int j=1; j<=n;j++){
            fin>>dist[i][j];
            if(i!=j && dist[i][j]==0) dist[i][j]=INF;
        }
    }

      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] > dist[i][k] + dist[k][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j =1;j<=n;j++){
            if(dist[i][j]!=INF) fout<<dist[i][j]<<" ";
            else fout<<0<<" ";
        }
        fout<<"\n";
    }
    fin.close();
    fout.close();
    return 0;
}