Cod sursa(job #3376853)

Utilizator stefan_16Raicu Stefan-Silviu stefan_16 Data 20 noiembrie 2026 08:43:58
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100;
int dist[NMAX + 1][NMAX + 1];

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

int main() {
    int n;
    fin >> n;
    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] = 1e9;
        }
    }
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++) {
                if(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++) {
            fout << dist[i][j] << " ";
        }
        fout << "\n";
    }
}