Cod sursa(job #2978970)

Utilizator CristeaCristianCristea Cristian CristeaCristian Data 14 februarie 2023 18:15:38
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>

using namespace std;
ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");
const int NMAX = 105, INF = 1e9;
int d[NMAX][NMAX], cost[NMAX][NMAX];
int main()
{
    int n, i, j, k;
    cin >> n;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            d[i][j] = INF;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j])
                d[i][j] = min(d[i][j], cost[i][j]);
        }
    for(k = 1; k <= n; k++)
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                if(d[i][k] != INF && d[k][j] != INF && i != j)
                    d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            if(d[i][j] == INF)
                d[i][j] = 0;
            cout << d[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}