Cod sursa(job #3336402)

Utilizator GridanAntoniaGridan Antonia GridanAntonia Data 24 ianuarie 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>
#include <vector>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int main()
{
    int n;
    fin >> n;
    int inf = 1e9+1;
    vector<vector<int>>ma(n+1, vector<int>(n+1));
    vector<vector<int>>dist(n+1, vector<int>(n+1, inf));

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

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

        }

    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(dist[i][j] == inf)
                fout << 0 << " ";
            else
            fout <<  dist[i][j] << " ";
        }
        fout << "\n";
    }
    return 0;
}