Cod sursa(job #3376843)

Utilizator Bonta-VladBonta Vlad Bonta-Vlad Data 20 noiembrie 2026 08:41:35
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
using namespace std;
const string io_file = "royfloyd";
const int NMAX = 100;

int dist[NMAX+1][NMAX+1];

int main() {
    ifstream cin (io_file + ".in");
    ofstream cout(io_file + ".out");
    
    int n;
    cin >> n;
    for (int i = 1 ; i <= n; i++) {
        for (int j = 1 ; j <= n; j++) {
            cin >> 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++) {
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}