Cod sursa(job #3322499)

Utilizator antonia225Stoica Elena-Antonia antonia225 Data 14 noiembrie 2025 15:07:36
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int main()
{
    int n;
    const int INF = 1e9;
    fin >> n;

    vector<vector<int>> d(n + 1, vector<int>(n + 1));

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            fin >> d[i][j];   
            if (d[i][j] == 0 && i != j)
                d[i][j] = INF;
        }

    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (d[i][j] > d[i][k] + d[k][j])
                    d[i][j] = d[i][k] + d[k][j];

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (d[i][j] == INF)
                fout << 0 << " ";
            else 
                fout << d[i][j] << " ";
        }
        fout << '\n';
    }

    return 0;
}