Cod sursa(job #2354618)

Utilizator OvidiuDestulDeOkNicoleanu Ovidiu OvidiuDestulDeOk Data 25 februarie 2019 13:39:18
Problema Floyd-Warshall/Roy-Floyd Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#define INF 101
#define NMAX 101

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

int n;
int D[NMAX][NMAX];

void read();
void run();
void print();

int main(){
    read();
    run();
    print();
    return 0;
}

void read(){int i, j;
    fin >> n;
    for(i = 1 ; i <= n; i++)
        for(j = 1; j <= n; j++){
            fin >> D[i][j];
            if(!D[i][j] && i != j) D[i][j] = INF;
        }

}

void run(){int x, y, z;
    for(z = 1; z <= n; z++)
    for(x = 1; x <= n; x++)
    for(y = 1; y <= n; y++)
    if(D[x][y] > D[x][z] + D[z][y])
        D[x][y] = D[x][z] + D[z][y];
}

void print(){int i, j;
    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++)
            if(D[i][j] != INF) fout << D[i][j] << ' ';
            else fout << "0 ";
        fout << '\n';
    }
    fout.close();
}