Cod sursa(job #3213751)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 13 martie 2024 13:32:07
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
using pii = pair<int,int>;
ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");
const int nmax = 1e2 + 1;
int c[nmax][nmax] , n;
signed main()
{
    cin >> n;
    for(int i = 1 ; i <= n ; ++i)
    {
        for(int j = 1 ; j <= n ; ++j)
        {
            cin >> c[i][j];
            if(c[i][j] == 0 && i!=j) c[i][j] = 1e9;
        }
    }
    for(int k = 1 ; k <= n ; ++k)
    {
        for(int i = 1 ; i <= n ; ++i)
        {
            for(int j = 1 ; j <= n ; ++j)
            {
                c[i][j] = min(c[i][j],c[i][k] + c[k][j]);
            }
        }
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        for(int j = 1 ; j <= n ; ++j)
        {
            if(c[i][j] == 1e9) c[i][j] = 0;
        }
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        for(int j = 1 ; j <= n ; ++j) cout << c[i][j] << ' ';
        cout << '\n';
    }
    return 0;
}