Cod sursa(job #2740564)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 13 aprilie 2021 16:00:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

#ifndef LOCAL

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

#define cin in
#define cout out

#endif //LOCAL

const int NMAX = 128;
int mat[NMAX][NMAX];

int main()
{
    int n; cin >> n;
    for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
    {
        cin >> mat[i][j];
        if(mat[i][j] == 0)
            mat[i][j] = 1e6;
        if(i == j)
            mat[i][j] = 0;
    }

    for(int k = 0; k < n; k++)
    for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
        mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
            cout << mat[i][j] << ' ';
        cout << '\n';
    }
}