Cod sursa(job #3375714)

Utilizator morarescuemanueleMorarescu Emanuele morarescuemanuele Data 13 noiembrie 2026 09:19:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <vector>
#include <fstream>
using namespace std;
const int NMAX=1e3;
int d[NMAX +1][NMAX+1];
ofstream fout("royfloyd.out");
ifstream fin("royfloyd.in");

int main()
{
    int n;
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            fin>>d[i][j];
            if(i!=j && d[i][j]==0)
            {
                d[i][j]=1e9;
            }
        }
    
    }
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(d[i][j]>d[i][k]+d[k][j])
                {
                    d[i][j]=d[i][k]+d[k][j];
                }
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(d[i][j] == 1e9)
            {
                fout << 0 << " ";
            }
            else
            {
                fout << d[i][j] << " ";
            }
        }
        fout << "\n";
    }

}