Cod sursa(job #3320795)

Utilizator stefanchpStefan Chiper stefanchp Data 7 noiembrie 2025 14:16:42
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#define infinit 1e9
using namespace std;

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

int n, m;
int a[105][105], c[105][105];

void citire()
{
    int x, y, cost;
    fin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            fin >> c[i][j];
            if (c[i][j] == 0 && i != j)
                c[i][j] = 1e9;
        }

}

void RoyFloyd()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                c[i][j] = c[i][k] + c[k][j] < c[i][j] ? c[i][k] + c[k][j] : c[i][j];
}

int main()
{
    citire();
    RoyFloyd();
    for (int i = 1; i <= n; i++, fout << '\n')
        for (int j = 1; j <= n; j++)
            if (c[i][j] != infinit)
                fout << c[i][j] << ' ';
            else fout << 0 << ' ';
    return 0;
}