Cod sursa(job #3165043)

Utilizator octavian202Caracioni Octavian Luca octavian202 Data 5 noiembrie 2023 12:24:04
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 103, INF = 1e7;
int dist[NMAX][NMAX];

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] = INF;
        }
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dist[i][j] = min(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)
                dist[i][j] = 0;
            fout << dist[i][j] << ' ';
        }
        fout << '\n';
    }

    return 0;
}