Cod sursa(job #3322346)

Utilizator RaresAnghelAnghel Rares Mihai RaresAnghel Data 13 noiembrie 2025 16:10:17
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
const int INF = 1e9;

int main()
{
    int n;
    f >> n;
    cout<<n;
    vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            int x;
            f >> x;
            if (i == j) dist[i][j] = 0;
            else if (x != 0) dist[i][j] = x;
            else dist[i][j]=INF;
        }
    }
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (dist[i][k] < INF && dist[k][j] < INF)
                    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++)
            if (dist[i][j]== INF) g << 0;
            else g << dist[i][j] << ' ';
        g << '\n';
    }
    return 0;
}