Cod sursa(job #2422372)

Utilizator CameliaSSamoilescu Camelia CameliaS Data 18 mai 2019 15:56:22
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
using namespace std;

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

#define inf 100005
#define NMAX 105

int n, dist[NMAX][NMAX];


// j, i+1
void citire(){
    f>>n;
    for(int i = 1 ; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            {
                f>>dist[i][j];
                if(i!j && dist[i][j] == 0)
                    dist[i][j] = inf;
            }

}

void RoyFloyd(){

    for(int k = 1; k <= n; k ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <=n; j++)
                if(dist[i][k] + dist[k][j] < dist[i][j] )
                    dist[i][j] = dist[i][k] + dist[k][j];


}

void afisare(){
    for(int i = 1 ; i <= n; i ++)
       {
        for(int j = 1; j <= n; j ++)
            if(dist[i][j] == inf)
                g<<'0'<<" ";
        else
                g<<dist[i][j]<< " ";
        g<<'\n';
       }
}

int main()
{
    citire();
    RoyFloyd();
    afisare();
    return 0;
}