Cod sursa(job #2626752)

Utilizator VictorVrabieVrabie Victor VictorVrabie Data 8 iunie 2020 10:42:00
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <cstdio>
#include <algorithm>

int dp[128][128];
int n{};



int main(){

    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);

    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
            scanf("%d", &dp[i][j]);
    }

    for (int k = 0; k < n; ++k) // phase loop - dp[i][j]  for any vertices i and j stores the length of the shortest path 
                               // between the vertex i and vertex j, which contains only the vertices {1,2,...,k?1} 
                               //as internal vertices in the path

    {
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                if (dp[i][k] && dp[k][j])
                    dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k][j]);
    }
    

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
            printf("%d ", dp[i][j]);
        printf("\n");
    }


		
	return 0;
}