Cod sursa(job #1166566)

Utilizator muresan_bogdanMuresan Bogdan muresan_bogdan Data 3 aprilie 2014 17:49:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<iostream>
#include<fstream>
using namespace std;

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

const int INF = 1000000000;
int n, i, j, k;
int dist[101][101];

int main() {
    fin >> n;
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            fin >> dist[i][j];
            if((dist[i][j] == 0) && (i != j)) {
                    dist[i][j] = INF;
            }
        }
    }
    for(k = 0; k < n; k++) {
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                if (dist[i][j] > dist[i][k] + dist[k][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            if(dist[i][j] == INF) {
                fout << "0 ";
            }else {
                fout << dist[i][j] << ' ';
            }
        }
        fout << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}