Cod sursa(job #3299947)

Utilizator jumaracosminJumara Cosmin-Mihai jumaracosmin Data 11 iunie 2025 23:26:17
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

//#define fin std::cin
//#define fout std::cout

/*
    -> Roy Floyd / Floyd Warshall
    --------
    Sa se determine matricea costurilor drumurilor RF[i][j] = costul minim al unui drum
    de la nodul i la nodul j

    Complexitate: O(n^3) timp, O(n^2) memorie
*/

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

const int nmax = 1e3 + 2;
const int inf = 1e9;

int n;
int cost[nmax][nmax];
int royFloyd[nmax][nmax];

int main()
{
    fin >> n;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            fin >> cost[i][j];

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            royFloyd[i][j] = cost[i][j];

    
    for(int aux = 1; aux <= n; ++aux) // drumul i -> j sa fie de forma i -> aux -> j
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                royFloyd[i][j] = std::min(royFloyd[i][aux] + royFloyd[aux][j], royFloyd[i][j]);

    for(int i = 1; i <= n; ++i, fout << "\n")
        for(int j = 1; j <= n; ++j)
            fout << royFloyd[i][j] << " ";
    
    return 0;
}