Cod sursa(job #3275487)

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

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

const int oo = 1e9;

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(i != j && m[i][j] == 0)
                m[i][j] = oo;
        }
}

void royfloyd() {
    for(int temp = 1; temp <= n; temp ++)
        for(int src = 1; src <= n; src ++)
            for(int dest = 1; dest <= n; dest ++)
                    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 ++)
            fout << m[i][j] << " ";
}

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