Cod sursa(job #2343538)

Utilizator AlexandruBrezuleanuAlexandruBrezuleanu AlexandruBrezuleanu Data 14 februarie 2019 08:47:22
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#define MAX 1010
#define INF 10000000

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

int cost[MAX][MAX];
int dp[MAX][MAX];
int n;
void citire();
void rf();
void afisare();
int main()
{
    citire();
    rf();
    afisare();
    return 0;
}
void citire()
{
    int i, j;
    fin>>n;
    for (i=1; i<=n; i++)
        for (j=1; j<=n; j++)
            fin>>cost[i][j];
}

void rf()
{
    int i, j, k;
    for (i=1; i<=n; i++)
        for (j=1; j<=n; j++)
            if (!cost[i][j])
               cost[i][j]=INF;
    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]);
}

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