Cod sursa(job #3245370)

Utilizator AndreiSorin26012001Cirpici Andrei Sorin AndreiSorin26012001 Data 28 septembrie 2024 18:06:34
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <vector>

int main()
{
    std::ifstream in("royfloyd.in");
    int n; in >> n;
    std::vector adjancencyMatrix(n, std::vector(n, 0));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            in >> adjancencyMatrix[i][j];
    in.close();

    std::vector dist(n, std::vector(n, 0));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            dist[i][j] = adjancencyMatrix[i][j];

    for(int k = 0; k < n; k++)
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                if(adjancencyMatrix[i][k] != 0 && adjancencyMatrix[k][j] != 0 && (dist[i][j] == 0 || dist[i][j] > dist[i][k] + dist[k][j]) && i != j)
                    dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);

    std::ofstream out("royfloyd.out");
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
            out << dist[i][j] << " ";
        out << "\n";
    }
    out.close();
    return 0;
}