Cod sursa(job #3130844)

Utilizator cygAlexandruIvanIvan Alexandru cygAlexandruIvan Data 18 mai 2023 18:18:25
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>

using namespace std;

const int N = 101, INF = 1001;
int cost[N+1][N+1];
int n;

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

void rf(){
    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)
            {
                cost[i][j] = 0;
            }

            out << cost[i][j] << ' ';
        }

        out << '\n';
    }
}

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

    rf();
    return 0;
}