Cod sursa(job #3130826)

Utilizator AndreiN96Andrei Nicula AndreiN96 Data 18 mai 2023 17:59:20
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

const int N = 100, INF = 1000001;
int cost[N + 1][N + 1];

int main()
{
    ifstream in("royfloyd.in");
    ofstream out("royfloyd.out");

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

    for (int k = 1; k <= n; k ++)
    {
        for (int i = 1; i <= n; i ++)
        {
            for (int j = 1; j <= n; j ++)
            {
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            }
        }
    }

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

    in.close();
    out.close();

    return 0;
}