Cod sursa(job #2978986)

Utilizator CristeaCristianCristea Cristian CristeaCristian Data 14 februarie 2023 18:22:29
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 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++)
            if(i != j)
                d[i][j] = INF;
            else
                d[i][j] = 0;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j])
                d[i][j] = cost[i][j];
        }
    for(k = 1; k <= n; k++)
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; 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;
}