Cod sursa(job #3197871)

Utilizator AlbertPavPavalache Albert AlbertPav Data 27 ianuarie 2024 16:32:29
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
using namespace std;
const int INF = 1000000;

int n, C[101][101];

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

int main()
{
    int cost;
    f >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            f >> C[i][j];
            if(C[i][j]==0)
                C[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 = C[i][k] + C[k][j];
                if(cost < C[i][j])
                    C[i][j] = cost;
            }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            if(C[i][j]!=INF)
                g << C[i][j] << ' ';
            else
                g<<"0 ";
        g << '\n';
    }
    return 0;
}