Cod sursa(job #2075815)

Utilizator maenstru56Peteleaza Darius maenstru56 Data 25 noiembrie 2017 18:03:35
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, w[105][105];

void citire(){
    in >> n;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            in >> w[i][j];
}

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(i != k && j != k && i != j && (w[i][k] + w[k][j] < w[i][j] || w[i][j] == 0))
                    w[i][j] = w[i][k] + w[k][j];
}

void afisare(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            out << w[i][j] << " ";
        out << '\n';
    }
}

int main()
{
    in.sync_with_stdio();
    out.sync_with_stdio();

    citire();
    roy_floyd();
    afisare();

    return 0;
}