Cod sursa(job #3357954)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 22:12:32
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

const int INF = 1000000000;

int dist[105][105];

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

    int n;
    fin >> n;

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

    fin.close();
    fout.close();

    return 0;
}