Cod sursa(job #3343140)

Utilizator gabi_vag742Vasile Alexandru Gabriel gabi_vag742 Data 26 februarie 2026 15:41:58
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;

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

const int inf = 9999999;
int n, g[105][105];

void citire()
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            fin >> g[i][j];
}

void initiere()
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            g[i][j] = inf;
}

int main ()
{
    fin >> n;

    initiere();
    citire();

    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(i != j && (g[i][k]+g[k][j] < g[i][j]) )
                    g[i][j] = g[i][k] + g[k][j];

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            if(g[i][j] == inf)
                fout << 0 << ' ';
            else
                fout << g[i][j] << ' ';

        fout << '\n';
    }

    return 0;
}