Cod sursa(job #1926876)

Utilizator AkrielAkriel Akriel Data 14 martie 2017 19:08:00
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

#define N 105

using namespace std;

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

int totalNodes;

int nodes[N][N];

inline void readVaraibles(void){
    fin >> totalNodes;
    for ( int indexY = 1; indexY <= totalNodes; indexY++ )
        for ( int indexX = 1; indexX <= totalNodes; indexX++ )
            fin >> nodes[indexY][indexX];
}

inline void royFloyd(void){
    for ( int indexK = 1; indexK <= totalNodes; indexK++ )
        for ( int indexY = 1; indexY <= totalNodes; indexY++ )
            for ( int indexX = 1; indexX <= totalNodes; indexX++ )
                if ( nodes[indexY][indexK] and nodes[indexK][indexX] )
                    if ( nodes[indexY][indexX] > nodes[indexY][indexK] + nodes[indexK][indexX] or !nodes[indexY][indexX])
                        if ( indexX != indexY )
                            nodes[indexY][indexX] = nodes[indexY][indexK] + nodes[indexK][indexX];
}

inline void printSolution(void){
    for ( int indexY = 1; indexY <= totalNodes; indexY++ ){
        for ( int indexX = 1; indexX <= totalNodes; indexX++ )
            fout << nodes[indexY][indexX] << " ";
        fout << "\n";
    }
}

int main(){
    readVaraibles();
    royFloyd();
    printSolution();
}