Cod sursa(job #3357231)

Utilizator Andreea1Bica Andreea Cristiana Andreea1 Data 7 iunie 2026 12:47:46
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <iostream>
#include <fstream>

using namespace std;

const int INF = 999999999;

int n, 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 < i; j++)
            C[i][j] = C[j][i] = INF;
    }
}*/

void citire()
{
    f >> n;
    //init();
    int x;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            f >> C[i][j];
            //if(x != 0)
               // C[i][j] = x;
        }
}

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

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();
    f.close();
    g.close();
    return 0;
}