Cod sursa(job #2726145)

Utilizator elena_dElena Dulgheru elena_d Data 20 martie 2021 14:03:19
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
using namespace std;
ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");
const int N = 101, INF = 1000001;
int cost[N][N], n;
void royfloyd()
{
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (cost[i][j] > cost[i][k] + cost[k][j])
                {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
            }
        }
    }
}
int main()
{
    int i, j;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            cin >> cost[i][j];
            if (i != j && cost[i][j] == 0)
            {
                cost[i][j] = INF;
            }
        }
    }
    royfloyd();
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (cost[i][j] == INF)
                cout << 0 << ' ';
            else
                cout << cost[i][j] << ' ';
        }
        cout << "\n";
    }

    return 0;
}