Cod sursa(job #1922970)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 10 martie 2017 20:00:23
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <iostream>
#include <vector>

#define maxn 101
#define INF 0x3f3f3f3f

using namespace std;

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

int n;
int dist[maxn][maxn];

void RoyFloyd(){
    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){
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
    }
}

int main(){
    f >> n;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            f >> dist[i][j];
            if(dist[i][j] == 0) dist[i][j] == INF;
        }
    }
    RoyFloyd();
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            g << (dist[i][j] == INF ? 0 : dist[i][j]) << ' ';
        g << '\n';
    }
}