Cod sursa(job #3131629)

Utilizator SergetecLefter Sergiu Sergetec Data 20 mai 2023 19:21:30
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
/*
 * Lefter Sergiu - 20/05/2023
*/
#include <fstream>

using namespace std;

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

const int N = 100;
const int INF = 199999999;

int n, cost[N + 1][N + 1];

void RoyFloyd() {
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (i != j && i != k && j != k && cost[i][k] + cost[k][j] < cost[i][j]) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
            }
        }
    }
}

int main() {
    in >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            in >> cost[i][j];
            if (i != j && cost[i][j] == 0) {
                cost[i][j] = INF;
            }
        }
    }
    RoyFloyd();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i != j && cost[i][j] == INF) {
                cost[i][j] = 0;
            }
            out << cost[i][j] << " ";
        }
        out << '\n';
    }
    in.close();
    out.close();
    return 0;
}