Cod sursa(job #2645675)

Utilizator Marius7122FMI Ciltea Marian Marius7122 Data 29 august 2020 13:58:11
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
const int N = 105;

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

int n;
int dist[N][N];

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            fin >> dist[i][j];

    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            if(i != k)
                for(int j = 1; j <= n; j++)
                    if(i != j && dist[i][k] != 0 && dist[k][j] != 0)
                        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            fout << dist[i][j] << ' ';
        fout << '\n';
    }

	return 0;
}