Cod sursa(job #3357153)

Utilizator alessia_04Alessia alessia_04 Data 6 iunie 2026 17:48:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;

const int INF = 9999999;

int n;
int C[101][101];

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

void init()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i==j) C[i][j]=0;
            else C[i][j]=INF;
}

void citire()
{
    f >> n;

    init();

    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x;
            f >> x;

            if(x != 0)
                C[i][j] = x;
        }
}

void Roy_Floyd()
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(C[i][k] != INF && C[k][j] != INF)
                    if(C[i][j] > C[i][k] + C[k][j])
                        C[i][j] = C[i][k] + C[k][j];
}

void afis()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(C[i][j] == INF)
                g << 0 << " ";
            else
                g << C[i][j] << " ";
        }
        g << '\n';
    }
}

int main()
{
    citire();
    Roy_Floyd();
    afis();
}