Cod sursa(job #2209873)

Utilizator GeorgianBaditaBadita Marin-Georgian GeorgianBadita Data 4 iunie 2018 22:33:50
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#define NMAX 105
#define INF 1e9
using namespace std;

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

int n;
int dist[NMAX][NMAX];

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

void roy_floyd(){
    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 print_sol(){
    for(int i = 1; i<=n; i++){
        for(int j = 1; j<=n; j++){
            dist[i][j] != INF ? g << dist[i][j] << ' ' : g << 0 << ' ';
        }
        g << '\n';
    }
}

int main(){
    read_data(n);
    roy_floyd();
    print_sol();
    return 0;
}