Cod sursa(job #2343526)

Utilizator AlexandruBrezuleanuAlexandruBrezuleanu AlexandruBrezuleanu Data 14 februarie 2019 08:23:57
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#define MAX 1010
#define MAX1 101000

using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int cost[MAX][MAX];
int n;

int main()
{
    int i, j, k;

    fin >> n;
    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            fin >> cost[i][j];

    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            if(!cost[i][j])
                cost[i][j] = MAX1;

    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(k!=i && k!=j && i!=j)
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
            if(cost[i][j] == MAX1)
                fout<< 0 << ' ';
            else fout << cost[i][j] << ' ';

        fout<<'\n';
    }
    return 0;
}