Cod sursa(job #3040307)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 29 martie 2023 18:33:16
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

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

const int max_size = 1e2 + 1, INF = 1e9 + 1;

int a[max_size][max_size];

int main ()
{
    int n;
    in >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            in >> a[i][j];
            if (a[i][j] == 0)
            {
                a[i][j] = 0;
            }
        }
    }
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (a[i][j] == INF)
            {
                out << 0 << " ";
            }
            else
            {
                out << a[i][j] << " ";
            }
        }
        out << '\n';
    }
    in.close();
    out.close();
    return 0;
}