Cod sursa(job #3192643)

Utilizator Ionut10Floristean Ioan Ionut10 Data 13 ianuarie 2024 09:30:09
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

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

const int NMAX = 100;
const int INF = 1e8;
int n;
long long c[NMAX + 1][NMAX + 1];

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
        fin >> c[i][j];
        if(c[i][j]==0)
            c[i][j] = INF;
    }
    for (int z = 1; z <= n; z++)
        for (int x = 1; x <= n; x++)
            for (int y = 1; y <= n; y++){
                if ( c[x][z] + c[z][y] < c[x][y])
            c[x][y] = c[x][z] + c[z][y];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            if ( (i == j) ||c[i][j] >= INF)
                fout << 0 << ' ';
            else fout << c[i][j] << ' ';
        fout << '\n';
    }
    return 0;
}