Cod sursa(job #2735972)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 3 aprilie 2021 00:29:52
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
const int maxn = 105;
int dist[maxn][maxn];
int M[maxn][maxn];
int main()
{
    int n;
    in >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            in >> M[i][j];
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(i != j)
            {
                if(M[i][j] != 0)
                    dist[i][j] = M[i][j];
                else
                    dist[i][j] = (1 << 29);
            }
        }
    }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(dist[i][j] > dist[i][k] + dist[k][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
    for(int i = 1; i <= n; i++, out << "\n")
        for(int j = 1; j <= n; j++)
            out << dist[i][j] << " ";
    return 0;
}