Cod sursa(job #3271621)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 26 ianuarie 2025 18:28:40
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <fstream>

using namespace std;

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


int n;

int main()
{
    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];

    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (i != j && d[i][k] && d[k][j]) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

                    if (d[i][j] == 0)
                        d[i][j] = d[i][k] + d[k][j];
                }
    for (int i = 1; i <= n; i++, fout << "\n")
        for (int j = 1; j <= n; j++)
            fout << d[i][j] << " ";
}