Cod sursa(job #2715420)

Utilizator Rares09Rares I Rares09 Data 3 martie 2021 17:42:41
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

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

int n, m, w[105][105];

int main()
{
    cin >> n;

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin>>w[i][j];
            if(w[i][j]==0 && i!=j)
                w[i][j]=1e9;
        }
    }

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            for(int q = 1; q <= n; ++q)
            {
                if(w[i][q] < 1e9 && w[q][j] < 1e9)
                    w[i][j] = min(w[i][j], w[i][q] + w[q][j]);
            }
        }
    }

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cout << w[i][j] << ' ';
        }

        cout << '\n';
    }

    return 0;
}