Cod sursa(job #2611414)

Utilizator iuliap1999Iulia Popa iuliap1999 Data 6 mai 2020 20:32:19
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;

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

//int n,a[105][105],i,j,x,k;
const int NMAX = 105;
const int INF = 1e9;
int n, dist[NMAX][NMAX];

int main()
{
    f >> n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dist[i][j] = INF;
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            int x;
            f >> x;
            if(x != 0) {
                dist[i][j] = x;
            }
        }
    }
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(dist[i][k] + dist[k][j] < dist[i][j] && i != j) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(dist[i][j] == INF) {
                g << 0 << ' ';
            } else {
                g << dist[i][j] << ' ';
            }
        }
        g << '\n';
    }
    return 0;
}