Cod sursa(job #2972611)

Utilizator NeacsaDenisDenis Neacsa NeacsaDenis Data 29 ianuarie 2023 20:20:44
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

const int INF = 9999999;

int n, A[101][101];

void Init()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j < i; j++)
            A[i][j] = A[j][i] = INF;
    }
}

void Citire()
{
    f >> n;
    Init();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            f >> A[i][j];
}

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 = A[i][k] + A[k][j];
                if(cost < A[i][j])
                    A[i][j] = cost;
            }
}

void Afisare()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            g << A[i][j] << ' ';
        g << '\n';
    }
}

int main()
{
    Citire();
    Roy_Floyd();
    Afisare();
    f.close();
    g.close();
    return 0;
}