Cod sursa(job #2978998)

Utilizator VladS23Vlad Seba VladS23 Data 14 februarie 2023 18:29:34
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>

using namespace std;

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

const int NMAX = 100;
const int INF = 1e9;
int d[NMAX + 5][NMAX + 5], c[NMAX + 5][NMAX + 5];
int n;

int main()
{
    for (int i = 1; i <= NMAX; i++)
    {
        for (int j = 1; j <= NMAX; j++)
        {
            if (i != j)
                d[i][j] = INF;
            else
                d[i][j] = 0;
        }
    }
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            int aux;
            cin >> aux;
            if (aux)
                d[i][j] = aux;
        }
    }
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (d[i][j] == INF)
                d[i][j] = 0;
            cout << d[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}