Cod sursa(job #2766007)

Utilizator NanuGrancea Alexandru Nanu Data 30 iulie 2021 18:41:20
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");

#define DIM 101
#define INF (1 << 25)

int v[DIM][DIM], n, i, j, cost[DIM][DIM];

static inline void Floyd() {
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            if(v[i][j] || i == j)   ///daca am drum intre ele sau e chiar nodul curent;
                cost[i][j] = v[i][j];
            else cost[i][j] = INF;

    for(int k = 1; k <= n; k++)
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                if(cost[i][j] > cost[i][k] + cost[k][j])    ///daca gasesc un cost mai mic;
                    cost[i][j] = cost[i][k] + cost[k][j];
}

int main() {
    cin >> n;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            cin >> v[i][j];

    Floyd();
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= n; j++)
            if(cost[i][j]  == INF)  ///nu am drum intre ele;
                cout << 0 << " ";
            else cout << cost[i][j] << " ";
        cout << '\n';
    }

    return 0;
}