Cod sursa(job #3378092)

Utilizator ilinca_onofreiIlinca-Ioana Onofrei ilinca_onofrei Data 25 noiembrie 2026 11:29:06
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int INF = 1e9;
int dist[105][105];

int main() {
    int n;
    if (!(fin >> n)) return 0;

    // citesc matricea si transform valorilor 0 (fara muchie) in INF
    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] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
    }

    // afis matricea (0 daca nu exista drum)
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (dist[i][j] == INF) {
                fout << 0 << " ";
            } else {
                fout << dist[i][j] << " ";
            }
        }
        fout << "\n";
    }

    return 0;
}