Cod sursa(job #3275480)

Utilizator witekIani Ispas witek Data 10 februarie 2025 19:33:55
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

const int INF = 2e9;

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

int n;
int m[101][101];

void read() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> m[i][j];
            if (m[i][j] == 0 && i != j) {
                m[i][j] = INF;
            }
        }
    }
}

void royfloyd() {
    for (int temp = 1; temp <= n; temp++) {
        for (int src = 1; src <= n; src++) {
            for (int dest = 1; dest <= n; dest++) {
                if (m[src][temp] != INF && m[temp][dest] != INF) {
                    m[src][dest] = min(m[src][dest], m[src][temp] + m[temp][dest]);
                }
            }
        }
    }
}

void print() {
    for (int i = 1; i <= n; i++, fout << '\n') {
        for (int j = 1; j <= n; j++) {
            if (m[i][j] == INF)
                fout << "0 ";
            else
                fout << m[i][j] << " ";
        }
    }
}

int main() {
    read();
    royfloyd();
    print();
}