Cod sursa(job #3183569)

Utilizator SorinBossuMarian Sorin SorinBossu Data 12 decembrie 2023 11:54:13
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <vector>
#include <fstream>

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

constexpr int nmax= 1001;
const int OO = 1e9;
std::vector<std::pair<int, int>> adj[nmax];

int dist[nmax][nmax];
int n,m;

int main()
{
    in >> n;
    for ( int i = 1; i <= n; ++i )
        for ( int j = 1; j <= n; ++j ){
            in >> dist[i][j];
        }

           for ( int k = 1; k <= n; ++k )
    for ( int i = 1; i <= n; ++i )
        for ( int j = 1; j <= n; ++j )
             if ( dist[i][k] and dist[k][j] and ( dist[i][k] + dist[k][j] < dist[i][j] or !dist[i][j]) and i != j )
              dist[i][j] = dist[i][k] + dist[j][k];

    for ( int i = 1; i <= n; ++i, out << "\n")
        for ( int j = 1; j <= n; ++j )
            out << dist[i][j] << " ";

    return 0;
}