Cod sursa(job #3265713)

Utilizator stefan0211Tacu Darius Stefan stefan0211 Data 2 ianuarie 2025 16:25:40
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;

ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

const int VAL_MAX = 1001;

int main()
{
    int n;
    f >> n;

    vector<vector<int>> matrice_costuri(n + 1, vector<int>(n + 1, 0));

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            f >> matrice_costuri[i][j];
            if (matrice_costuri[i][j] == 0)
                matrice_costuri[i][j] = VAL_MAX;
        }

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

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (i == j || matrice_costuri[i][j] == VAL_MAX)
                g << 0 << ' ';
            else
                g << matrice_costuri[i][j] << ' ';
        }
        g << '\n';
    }
    return 0;
}