Cod sursa(job #3308412)

Utilizator DasapSapunaru Daniel Dasap Data 24 august 2025 15:52:45
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin ("royfloyd.in");
ofstream fout ("royfloyd.out");
const int INF = 1e9;
int n, cost[101][101], dist[101][101], i, j, k;
int main()
{
    cin >> n;
    for (i = 1; i <= n; i++) for (j = 1; j <= n; j++)
        {
            cin >> cost[i][j];
            dist[i][j] = cost[i][j];
            if(i==j)continue;
            if (cost[i][j] == 0) dist[i][j] = INF;
        }
    for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++)
                dist[i][j] = min (dist[i][j], dist[i][k] + dist[k][j]);
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (dist[i][j] == INF) dist[i][j] = 0;
            cout << dist[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}